LESSON 1: SETUP

1.1 Installing R and RStudio

For this course, there are two (free) associated programs you will need to install: R and RStudio. R is an opensource programming language designed for statistical computing. It is widely used by ecologists for quantitative analyses. RStudio is an environment for R that is more user-friendly than the standard R environment, so it is the environment we will use in this class. We will also install an addition set of tools, RTools, which is needed to run one of the packages that we will use in this class.

Installing R

To install R, first go to the R website. Under the “Getting Started” heading, you should see a link to download R. Click on this link, and it will take you to a page to select a CRAN mirror (a webserver that stores versions of R). You will want to select the CRAN mirror at the nearest location, so scroll down to the list of USA locations and click on the link for Iowa State University. On the new page, choose the correct R download for your system (Linux, Mac, or Windows) from the “Download and Install R” box at the top of the page. Details for each are below.

Linux:

Each of the Linux options (Debian, Fedora/Redhat, or Ubuntu) will provide you with the commands you need to run to install the latest version of R. If you have problems with the installation, please get in touch with me.

Mac:

On the new page, you should see a list of R packages, with the most up-to-date packages first. The package you will want depends on your operating system. The newest version is for macOS 11 and higher. If you are running an older version, select the appropriate package based on the system information provided for each package. When you click on the link for the package you need, it will prompt you to download the application setup file. Be sure to pay attention to where you save the file. Once you have downloaded the file, open the file and follow the installation instructions. You should not need to change any of the default installation settings. When you finish the installation process, you should by ready to run R on your computer.

Windows:

On the new page, select the “base” option from the list of subdirectories. At the very top of the next page, there will be a download link for the latest version of R. Select this link, and it will prompt you to download the application setup file. Be sure to pay attention to where you save the file. Once you have downloaded the file, open the file and follow the installation instructions. You should not need to change any of the default installation settings. When you finish the installation process, you should by ready to run R on your computer.

Installing RStudio

To install RStudio, first go to the RStudio Website. In the upper right-hand corner, hit the “Download RStudio” button. On the new page, scroll down to “RStudio Desktop” and select the “Download RStudio” button. Scroll down to “Step 2: Install RStudio Desktop”. If you are running Windows on your computer, there is a button to “Download RStudio Desktop for Windows” just below this heading. For other operating systems or old versions of Windows, continue scrolling down. Under the “All Installers and Tarballs” heading, there is a link to older versions of RStudio for Windows if you need that. Below that is a list of downloads for all operating systems. Select the appropriate system for your computer and save the application setup file. Finally, open the setup file and follow the installation instructions. When you finish the installation, RStudio should be ready to run on your computer.

Installing Rtools

Some R packages, which provide additional useful functions for analysis, are only available as source code, and they have to be compiled before they can be used. There are some additional steps we will need to take to make sure your computers can compile the code. Before completing these steps, be sure that R and RStudio are closed on your computer.

Mac:

If you have a Mac, you do not actually need to download RTools. Instead, open a terminal window (click the spotlight search button on your computer, and search for “Terminal”). Then copy and paste the code below into the terminal and hit “return”.

xcode-select –install

You might need to enter your computer password to allow the installation. Next, follow any onscreen instructions and allow the installation to finish. Once this is done, you should be good to go!

Windows:

If you have a PC, you will need to install RTools. Go to the RTools download website. Unless you are running an old version of R (you probably are not, unless you installed R years ago and have not updated), select the “RTools 4.4” link. On the new page, look for the Rtools44 installer link about halfway down the page, in the Installing Rtools 44 section. Click on this link to download the exe file. Run the file and follow the onscreen instructions to complete the installation. You may use the default settings. After the installation has finished, you should be set!

1.2 Layout of RStudio

To run R via RStudio, simply open the RStudio application. It will automatically connect with R, so you do not need to open R separately. When you open RStudio, the page should look something like this:

Image of RStudio layout, showing the console, environment, and help tabs

There are three main panels that you will see. The panel on the left panel is the console: the place where you will run your code and view the output. R is a programming language, so rather than selecting options from drop-down menus, you will be writing code that you will run to complete your statistical analyses.

The two panels on the right will not be used to write code, but they can help you stay organized and keep track of what you are doing. The upper right-hand panel is primarily for viewing the objects (such as variables and data frames; if this still doesn’t make sense, you will learn more about objects later) you create as you run your programs. You can view all the objects you have saved, what they are named, and what type of objects they are. The bottom right-hand panel has multiple tabs with different purposes. Here are the ones you should be familiar with: 1. Files: This tab has a file manager, similar to the file explorer on your computer. Here you can view the files in your directory. 2. Plots: When you create a plot, this is where you can view it. 3. Packages: R has packages you can install, which will give you access to additional functions. This tab shows you the packages you have installed and the packages you can install. 4. Help: Unsure what a function does or what arguments you need to include in a function? You can search for information about functions and other aspects of R in the Help tab.

1.3 R script files

As described above, you will be running your code in RStudio’s console. However, when you type code directly into the console, you cannot save that code. Therefore, you can instead write your code in an R script file, which is essentially a text file for writing R code. You can run code directly from the R script files and save the files so you can continue to work on them and run them at a later time. They are also a good resource for future projects. Often, we will end up using similar programs for multiple projects, so if we save our code, we can use it as a starting point for future project, instead of starting from nothing. I recommend that you ALWAYS code in a script file, instead of the console, unless you know it’s just a one-off command.

To open a script file, go to the file menu at the top of the window, hover over “New File”, and then select “R Script” from the new menu that appears. Now you will see a new panel on the top left-hand side of the screen where you can view and edit your script files. You can now type code into the script, save it, and run it. When you run the code, you will see it appear in the console.

To run the code from an R script, you can place your cursor in the line of code you want to run or highlight the code you want to run, then either click the run button (see picture below) or hit “ctrl + enter” (PC) or “cmd + return” (Mac). Alternatively, you can copy and paste the code from the script into the console.

Image of RStudio layout, showing the “run code” button at the top of an R script

When you write code in an R script, it is extremely helpful to include comments in your code, as a reminder of what you are doing in each step. To make a comment in your script file, first type #, followed by your comment. The # will tell R that the following text should not be run as code. In Lesson 2, you will see some examples of code comments.

1.4 Installing packages

One nice thing about R is that it is opensource software. This means it is free, but it also means that users can write packages for the program. These packages contain new functions that are not included in base R. Other users can install the packages, giving them access to the new functions. There are many, many R packages out there designed for different purposes. In this class, we will primarily use two packages, in addition to the base R functions.

Time to write your first piece of code in R!

The two packages we will use in this class are the tidyverse package (useful for data management and visualization; it is actually a set of several packages) and the lme4 package (useful for the type of statistical model we will use most in this class). We will install these two packages now.

This is one of the rare cases where it is fine to type the code directly into the console because once you install the packages, you won’t need to install them again unless you update R. It will just take a single line of code to install both packages. Put your cursor in the console, next to the “>” symbol and type the following lines of code (be sure to pay attention to punctuation and case):

install.packages("dplyr")
install.packages("ggplot2")
install.packages("lme4")

Then hit enter. You should see a bunch of text pop up in the console as the packages install. Once the packages install, you should see the “>” in the console again, indicating that you can type more code into the console. Sometimes warnings will show up in the console when you install packages, but they are usually nothing to worry about. We can deal with them on a case-by-case basis if necessary. If, after you run the line of code, you see only a few lines of text show up in the console, and then it stops, check for a dialogue box asking if you want to install the package in your personal library. If you get this message, click “yes”, and the packages should continue to install.

If you need to install other packages, you can just use the same install.packages() command and simply put the name of the package you need inside the parentheses.