Why R?

R is an extremely powerful statistics platform that is rapidly gaining popularity, particularly with natural sciences professionals for several reasons:

R uses code. Once you type the code and hit enter, it is entered into the system. The way to access that code is to label things and then pull things from the label. For example, if you run an ANOVA, you can label it X and then you can type X$residuals to access the residuals from the ANOVA. The problem with using just R is that once you are done with R, all of your code is gone unless you save every R session (this is called command line coding). You can use RStudio to save your code and submit it to R.

Downloading and installing R

Before installing RStudio, you need to install R. Can you do that here: https://www.r-project.org/ You will need to choose a CRAN mirror to install R. Please choose the location closest to you, which should be USA, Oregon State University. Once you click on that link it will bring you to the R download page options as shown below. From here you can choose to download R for Windows or Mac.

Figure 1. R download website showing the options for downloading R for Windows or Mac.


If you select Windows, click on the blue text that says install R for the first time on the next page, and then click Download R at the top of the page. If you selected Mac, choose the R program that matches your Mac OS (e.g. El Capitan, Mavericks). Always try to install the most recent version of R.

Downloading and installing RStudio

RStudio can be downloaded here: https://www.rstudio.com/. It is free. Check our this short video showing the four main windows of RStudio (scroll down to find the video): https://www.rstudio.com/products/rstudio/. The bottom left window is R. This is where you can write code on the command line, push enter, and run the code. You cannot save code here. The top left window is the Source editor where you can write (and save) your R code and then send it down to R (i.e., the output will appear in the bottom left window). The top right window is workspace that shows you all of the files you have in RStudio and the commands you ran. The bottom right window is where plots and figures show up. For any image you want to keep, click “Export, then Copy to clipboard, then copy as metafile” and then paste it into your word doc or powerpoint file. This bottom right window is also where you can install packages. Click on “Packages”, find your package and click to check the box next to the package name and it should automatically download. If the package you are looking for is not there, then click the “Install” button and search for your package by typing it in. You can also just use the code: install.pckages(“X”) to install packages. Note that “installing” packages does not mean that they are “loaded” into R. You need to also “load” the package using the code: library(“X”) where X = package name.

Setting up a working directory

You want to set up a folder into which you save all of your RStudio files and all of your excel files (TO.BE.EDITED.csv) that you are importing into R. You should name your working directory “Rfiles” and create it under your “Documents” folder. This directory has to be made before you submit the code below or you get an error message.

You can set your working directory using the window in the bottom right corner of RStudio (it has tabs named “Files”, “Plots”, etc.). If you select “Files” and find your Rfiles folder, click on the Rfiles folder, then click the “More” tab with the blue gear and select “Set As Working Directory”. This is the easiest way to set your working directory. You can also try using the code below.

Use the code below to set your working directory. To send this to R, highlight the line of code below (not the line that starts with ‘#’ but the setwd line of code), copy and paste it into the source editor window in RStudio (top left window). Highlight the line of code and then click the “Run” button on the top right of the source editor window and it should be sent to R (see image below for an example). Look into the R window below and you should see that the code was sent. You can also put your cursor beside the line of code and click Ctrl+Shift for a PC or Cmd+Shift for a Mac to send it to R.

#Use this code to set up your working directory
setwd('C:/Users/YourUserName/Documents/Rfiles')

For Mac Users:

setwd('/Users/YourUserName/Documents/Rfiles')

NOTE: When writing code in R, you can put a hashtag in front of it (#) and the line will be sent to R but R will not read the code. You can use this for putting notes above your code so that you know what it is for. For example, in the code above, the “#Use this code to set up your working directory” doesn’t actually run in R but you can keep it in RStudio.

Figure 2. Example of how code can be sent from the source editor window (top left) in RStudio to the R window (bottom left) in RStudio.


Throughout this website, you will be instructed to use code. That means copying the code from this website and pasting the code into the source editor window in RStudio. Code in the source editor window can be saved as an RStudio file that you can open up later. A good idea is to save the code with the name of the test you ran (i.e., if you run a t-test, save the RStudio file as “ttest”). There are many types of R files that you can save your code as. You want to save your code as an R script file. When you close RStudio down, you will be asked if you want to save the R script file (whatever you named it.R) and the Workspace image (.RData). You want to save any file with the “.R” extension as that is your source editor file (basically the code you wrote). You do not want to save the workspace image as that is all of the output ran in R (bottom left corner).

For more help with RStudio, check out their webpage, which has a lot of webinars and videos: https://www.rstudio.com/resources/webinars/

*Written July 2018.