1.3 First Time Working in R and RStudio
This next section provides a few notes on using R and RStudio now that you have installed it. In this section, we cover the following materials:
- Using R as a calculator and assigning objects using
<-
- Setting your working directory and the
setwd()
function. - Creating and saving an R script
This section highlights important concepts from QSS chapter 1.
1.3.1 Open RStudio
RStudio is an open-source and free program that greatly facilitates the use of R, especially for users new to programming. Once you have downloaded and installed R and RStudio, to work in R, all you need to do now is open RStudio (it will open R). It should look like this, though your version numbers will be different:
Note: The first time you open RStudio, you likely only have the three windows above. We will want to create a fourth window by opening an R script to create the fourth window.
- To do this, in RStudio, click on File -> New -> R script in your computer’s toolbar. This will open a blank document for text editing in the upper left of the RStudio window. We will return to this window in a moment.
- You can alternatively click on the green + sign indicator in the top-left corner of the RStudio window, which should give you the option to create a new R script document.
Now you should have something that looks like this, similar to Figure 1.1. in QSS:
- The upper-left window has our script document that will contain code.
- The lower-left window is the console. This will show the output of the code we run. We will also be able to type directly in the console.
- The upper-right window shows the environment (and other tabs, such as the history of commands). When we load and store data in RStudio, we will see a summary of that in the environment.
- The lower-right window will enable us to view plots and search help files, among other things.
1.3.2 Using R as a Calculator
The bottom left window in your RStudio is the Console. You can type in this window to use R as a calculator or to try out commands. It will show the raw output of any commands you type. For example, we can try to use R as a calculator. Type the following in the Console (the bottom left window) and hit “enter” or “return” on your keyboard:
5 + 3
## [1] 8
5 - 3
## [1] 2
5^2
## [1] 25
5 * 3
## [1] 15
5/3
## [1] 1.666667
5 + 3) * 2 (
## [1] 16
In the other RStudio windows, the upper right will show a history of commands that you have sent from the text editor to the R console, along with other items. The lower right will show graphs, help documents and other features. These will be useful later in the course.
1.3.3 Working in an R Script
Earlier, I asked you to open an R script in the upper left window by doing File, then New File, then R Script. Let’s go back to working in that window.
Set your working directory setwd()
(Almost) every time you work in RStudio, the first thing you will do is set your working directory. This is a designated folder in your computer where you will save your R scripts and datasets.
There are many ways to do this.
- An easy way is to go to Session -> Set Working Directory -> Choose Directory. I suggest choosing a folder in your computer that you can easily find and that you will routinely use for this class. Go ahead and create/select it.
- Note: when you selected your directory, code came out in the bottom left Console window. This is the
setwd()
command which can also be used directly to set your working directory in the future. - If you aren’t sure where your directory has been set, you can also type
getwd()
in your Console. Try it now
## Example of where my directory was
getwd()
If I want to change the working directory, I can go to the top toolbar of my computer and use Session -> Set Working Directory -> Choose Directory or just type my file pathway using the setwd()
below:
## Example of setting the working directory using setwd().
## Your computer will have your own file path.
setwd("/Users/ktmccabe/Dropbox/Rutgers Teaching/")
Saving the R Script
Let’s now save our R script to our working directory and give it an informative name. To do so, go to File, then Save As, make sure you are in the same folder on your computer as the folder you chose for your working directory.
Give the file an informative name, such as: “McCabeWeek1.R”. Note: all of your R scripts will have the .R extension.
1.3.4 Preparing your R script
Now that we have saved our R script, let’s work inside of it. Remember, we are in the top-left RStudio window now.
- Just like the beginning of a paper, you will want to title your R script. In R, any line that you start with a # will not be treated as a programming command. You can use this to your advantage to write titles/comments. Below is a screenshot example of a template R script.
- You can specify your working directory at the top, too. Add your own filepath inside
setwd()
- Then you can start answering problems in the rest of the script.
- Think of the R script as where you write the final draft of your paper. In the Console (the bottom-left window), you can mess around and try different things, like you might when you are taking notes or outlining an essay. Then, write the final programming steps that lead you to your answer in the R script. For example, if I wanted to add 5 + 3, I might try different ways of typing it in the Console, and then when I found out
5 + 3
is the right approach, I would type that into my script.
1.3.5 Executing Commands in your R script
The last thing we will note in this initial handout is how to execute commands in your R script.
To run / execute a command in your R script (the upper left window), you can
- Highlight the code you want to run, and then hold down “command + return” on a Mac or “control + enter” on Windows
- Place your cursor at the end of the line of code (far right), and hit “command + return” on a Mac or “control + return” on Windows, or
- Do 1 or 2, but instead of using the keyboard to execute the commands, click “Run” in the top right corner of the upper-left window.
Try it: Type 5 + 3
in the R script. Then, try to execute 5 + 3
. It should look something like this:
After you executed the code, you should see it pop out in your Console:
5 + 3
## [1] 8
Note: The symbol # also allows for annotation behind commands or on a separate line. Everything that follows # will be ignored by R. You can annotate your own code so that you and others can understand what each part of the code is designed to do.
## Example
<- 5 + 3 # example of assigning an addition calculation sum53
1.3.6 Objects
Sometimes we will want to store our calculations as “objects” in R. We use <-
to assign objects by placing it to the left of what we want to store. For example, let’s store the calculation 5 + 3
as an object named sum53
:
<- 5 + 3 sum53
After we execute this code, sum53 now stores the calculation. This means, that if we execute a line of code that just has
sum53`, it will output 8. Try it:
sum53
## [1] 8
Now we no longer have to type 5 + 3
, we can just type sum53
. For example, let’s say we wanted to subtract 2 from this calculation. We could do:
- 2 sum53
## [1] 6
Let’s say we wanted to divide two stored calculations:
<- 5 + 5
ten <- 1 + 1
two / two ten
## [1] 5
The information stored does not have to be numeric. For example, it can be a word, or what we would call a character string, in which case you need to use quotation marks.
<- "professor for this course"
mccabe mccabe
## [1] "professor for this course"
Note: Object names cannot begin with numbers and no spacing is allowed. Avoid using special characters such as % and $, which have specific meanings in R. Finally, use concise and intuitive object names.
- GOOD CODE:
practice.calc <- 5 + 3
- BAD CODE:
meaningless.and.unnecessarily.long.name <- 5 + 3
While these are simple examples, we will use objects all the time for more complicated things to store (e.g., like full datasets!) throughout the course.
We can also store an array or “vector” of information using c()
<- c(3, 6, 8, 9)
somenumbers somenumbers
## [1] 3 6 8 9
Importance of Clean Code
Ideally, when you are done with your R script, you should be able to highlight the entire script and execute it without generating any error messages. This means your code is clean. Code with typos in it may generate a red error message in the Console upon execution. This can happen when there are typos or commands are misused.
For example, R is case sensitive. Let’s say we assigned our object like before:
<- 5 + 3 sum53
However, when we went to execute sum53
, we accidentally typed Sum53
:
Sum53
## Error in eval(expr, envir, enclos): object 'Sum53' not found
Only certain types of objects can be used in mathematical calculations. Let’s say we tried to divide mccabe
by 2:
/ 2 mccabe
## Error in mccabe/2: non-numeric argument to binary operator
A big part of learning to use R will be learning how to troubleshoot and detect typos in your code that generate error messages.