🖥 Interacting with R Studio

If you want a slower introduction than we are providing here, check out the short book, Getting Used to R, RStudio, and R Markdown. Ismay and Kennedy (2016) include screencast recordings that you can follow along and pause as you learn. They include an introduction to R Markdown, a tool used for reproducible research in R.

Using RStudio

Icons of R versus RStudio on your computer.

FIGURE 0.4: Icons of R versus RStudio on your computer.

Much as we don’t drive a car by interacting directly with the engine but rather by interacting with elements on the car’s dashboard, we won’t be using R directly but rather we will use RStudio’s interface. After you install R and RStudio on your computer, you’ll have two new programs (also called applications) you can open. Always work in RStudio and not directly in the R application.

Let’s get familiar with RStudio. Open up RStudio. You should see three panes, or panels, dividing the screen: the Console pane, the Files pane, and the Environment pane.

This is your workspace. Start with the big pane on the left:

There are three panels (or tabs) in this window, we’ll be focusing on the Console and Terminal. When you first start R, the Console gives you some information about your version of R. The Console is where you can type and run R code. For example, if you type 1 + 1 and hit return, the Console returns 2.

Look at the top right:

The main two tabs you’ll be using are Environment and Git (which is not yet visible). The Environment tab shows you the datasets and variables you currently have loaded into R. In this case, we loaded in a dataset with 3407 rows and 5 columns and a variable x equal to 5. For you, the Environment should be empty. Let’s change that. Go to your Console and type:

x <- 5

This assigned the value 5 to an object, x. <- is the operator used to assign values to objects in R. Now, hit return/enter and you should see a variable x equal to 5 in your Environment tab. You must always hit return/enter after typing a command, otherwise RStudio will not realize that you want R to execute the command. Look at the bottom right window:

The Files tab displays your computer’s file system. When you create a project later, this tab will automatically show the contents of your project’s folder. The Plots tab will show you a preview of any plots you make in RStudio.

Packages

R packages, also known as libraries, extend the power of R by providing additional functions and data.

Analogy of R versus R packages.

FIGURE 0.5: Analogy of R versus R packages.

R is like a new mobile phone: while it has a certain amount of features when you use it for the first time, it doesn’t have everything. R packages are like the apps you can download onto your phone.

Consider an analogy to Instagram. If you have a new phone and want to share a photo with friends. You need to:

  1. Install the app: Since your phone is new and does not include the Instagram app, you need to download the app. You do this only once. (You might need to do this again in the future when there is an update to the app.)
  2. Open the app: After you’ve installed Instagram, you need to open it. You need to do this every time you use the app.

The process is very similar for an R package. You need to:

Installing versus loading an R package

FIGURE 0.6: Installing versus loading an R package

  1. Install the package: This is like installing an app on your phone. Most packages are not installed by default when you install R and RStudio. Thus if you want to use a package for the first time, you need to install it. Once you’ve installed a package, you likely won’t install it again unless you want to update it to a newer version.
  2. “Load” the package: “Loading” a package is like opening an app on your phone. Packages are not “loaded” by default when you start RStudio. You need to “load” each package you want to use every time you restart RStudio.

Installing a Package

Install a package which we will need in RS1. At the Console pane within RStudio, type:

install.packages("tidyverse")

And press return/enter on your keyboard. Note you must include the quotation marks around the name of the package. A package can depend on other packages, which will be automatically installed if needed.

Loading a Package

You have to load each package you want to use every time you start RStudio.

Recall that after you’ve installed a package, you need to “load” it. We do this by using the library() command.

For example, to load the remotes package, run the following code in the Console. What do we mean by “run the following code”? Either type or copy-and-paste the code into the Console and then hit the enter/return key.

After running the earlier code, a blinking cursor should appear next to the > symbol. (The > is the “prompt.”) This means you were successful and the remotes package is now loaded and ready to use. If, however, you get a red “error message” that reads:

Error in library(tidyverse) : there is no package called ‘tidyverse’

then you haven’t successfully installed the package. If you get this error message, make sure to install the tidyverse package before proceeding.

For historical reasons packages are also known as libraries, which is why the relevant command for loading them is library().

Note that R will occasionally ask you if you want to install some packages. Almost all the time you do want to, otherwise R would not be asking you.

Using a Package

You have to load each package you want to use every time you start RStudio. If you don’t load a package before attempting to use one of its features, you will see an error message like:

Error: could not find function

This is a different error message than the one you just saw about a package not having been installed yet. R is telling you that you are trying to use a function in a package that has not yet been loaded. R doesn’t know where to “find” the function you want to use.