Key Points

How we will use R

R is a popular free and open program for statistical computing (https://www.r-project.org/about.html). R can be run from the command line or from a graphical user interface that is automatically installed when you install R (if you open the R program installed on your computer, this is what will open up). We will be using a program called RStudio (see https://www.rstudio.com/about/) to interface with the R program. RStudio is what is known as an integrated development environment that makes it easier for R users to write and execute code and organize research projects. Some of the features that we can take advantage of are:

Today we will focus on setting up an RStudio project for the research project you will complete during this class and in the process, introduce you to how RStudio is set up and some basics of using and writing R code.

Setting up your research project

Open RStudio and click File > New Project. Make this project in a new directory and select “Empty Project”. You will be prompted to type a name for the project and choose a directory on your computer where you want to save it. The new project can be saved anywhere on your computer that makes sense and you should choose an informative project name with no spaces. If you think that you might want to track changes to this project using version control or save the code to GitHub, be sure to check the “Create a git repository” box.

You will now have three windows displayed in RStudio:

Let’s make a new R script- a text file where you save R code that performs an analysis. Click File > New File > R Script. This will open a new window with a tab for your R script called “Untitled1”. The first thing you should do when starting an analysis is to make a new R script and save it with a descriptive name (no spaces!). The best practice for project organization is to save all code scripts in a folder named “code”. In the Files window, click “New Folder” and make a folder named “code”. Then save your new script into this folder. Now you are ready to start an analysis!

How R works

Code vs. The Console

R is a fancy calculator that can remember previous calculations and store them for future use (see the section Objects and Functions). You can use R by typing commands directing into the console. For example, try doing some simple arithmetic. Place your cursor in the Console window in front of the > symbol and type 3 + 5. Then press Enter.

Exercise:

Convince yourself that R follows the order of operations.

Note: If you see a + symbol that means that R is waiting for further input from you before it can finish a calculation. Hit Escape if you aren’t sure what it is waiting for and want to try again.

Typing code into the console is inefficient and doesn’t allow you to easily save your work or edit previous work. Therefore you should always type R code into an R script and then run the code in the script. The R script is saved as a text file on your computer that you can open again later or send to colleagues.

Try typing some arithmetic expressions into the blank R script that you just made. Note each line corresponds to a new command to be sent to the R console. What happened in the R console after you typed the lines? Hopefully nothing.

R code that you type in a script will not be executed unless you tell RStudio to run it.

  • To run a single line of code in a script place your cursor anywhere on the line and press Ctrl+Enter or Cmd+Enter or click the green “Run” arrow at the upper right of the script window.
  • To run a portion of a line of code, use your cursor to highlight just the part you want and then press Ctrl+Enter or Cmd+Enter or click the green “Run” arrow at the upper right of the script window.
  • To run multiple lines of code use your cursor to highlight all of the lines you want and then press Ctrl+Enter or Cmd+Enter or click the green “Run” arrow at the upper right of the script window.
  • To run all the code in a script click the “Source” button at the upper right of the script window. Note that the Console displays source('~/filepath/name-of-script.R'), but doesn’t give any output. The source() function allows you to run any code file without opening it in RStudio (which may be useful to you later).

Documenting your code

The most important part of you code are the comments that document what the code is supposed to do. This is most useful for reminding you of what you are trying to accomplish in an analysis as well as for anyone else who might need to read your code.

Any text in an R script that follows the # is interpretted as a comment- a piece of descriptive text that doesn’t cause R to do any calculations.

The best practice is to begin any R script with comments that give the title, author (with email), date, and short description of what the code does.

Exercise

Add a header to your R script describing what will eventually be contained in it.

Don’t forget to press save periodically! RStudio does not automatically save changes to documents.

Additional Materials

Data Carpentry ecology lessons:

Introduction to R

Starting with data.

Rather than reproduce the lesson materials here, you can find them at the links listed above.