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.
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. We will have a lesson on Git.
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!). Now you are ready to start an analysis!
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.
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).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 project title, a short description of what the code does, and perhaps the author name and relevant citation(s).
Exercise
Add a header to your R script describing what will eventually be contained in it.
In this class we will often be working with and modifying existing R code that others have written. Many scientists learn R by borrowing code and modifying it to suit their purposes. Therefore it is useful to start right away learning how to search for answers and troubleshoot.
Download this R script and save it in your code folder in the research project you created today. You will need to click the ‘Raw’ button and then download the text file that appears. Be sure to remove the .txt extension that may have been added when you saved the file to your computer so that the file ends in .R. Then, open the downloaded script in your current RStudio session.
This R script makes a new folder called ‘data’ and downloads a csv
file from our course website into this folder. The csv file contains
data on trees and snags (i.e., dead trees) sampled in burned and
unburned sites in the Pine Ridge of Nebraska. It then reads the data
into R and saves it as an object named raw_data. The
raw_data object is a dataframe, which is basically a table
where rows are observations and columns are attributes. It is a very
useful data structure for performing statistical analyses. The code then
goes on to calculate the mean value of diameter at breast height (DBH)
across all trees/snags in the dataset and count the number of each
species of tree/snag sampled. Run the code so that you can see how it
works.
Exercise:
- Save the R script you downloaded as a new R script named “analysis_test.R” into the code folder in this project.
- Copy a csv file from your computer containing data that you are interested in working with. If you don’t have a csv file yet, go to the Neon Data Portal and download precipitation data from the Toolik Lake Core site in Alaska.
- Modify your R script so that it reads the data file into R and calculates the maximum value in one of the columns.
If you get stuck, use the Help tab in RStudio as well as internet searches to figure out how to accomplish this task. Ask your classmates for help too!
Don’t forget to press save periodically! RStudio does not automatically save changes to documents.