1 Install R and RStudio

  1. Download R from the R website. At the top of the page – under the heading “Download and Install R” – you’ll see separate links for Windows users, Mac users, and Linux users.
  2. Install R by opening the downloaded application file.
  3. Download “RStudio Desktop” from the RStudio website. You DO NOT need a paid version of RStudio, just download the free version of the RStudio Desktop.
  4. Install RStudio by opening the downloaded application file.
  5. Open ONLY RStudio.


2 Default panels in RStudio

  • Console (entire left)
  • Environment/History (upper right)
  • Files/Plots/Packages (lower right) (note: file names and paths will be different on your machine)

console
RStudio start up


3 The R console


3.1 Input and ouput

One of the easiest things you can do with R is use it as a simple calculator, so it’s a good place to start. In the console panel, click the broom icon to clear the copyright information and then type 10 + 20 at the command prompt (>). In your console, you should see:


console
Working in the console


Although this kind of screen picture is nice for seeing how work looks in RStudio, it’s not very helpful for completing a tutorial. To make the tutorials more user friendly, the input and output information from the console will be shown in boxes like this:

10 + 20
## [1] 30

The text in the input box (10 + 20) is formatted so that you can copy and paste the text directly into your console and hit Enter. The text in the output box (## [1] 30) has two hash tags added to the front of the output. This additional formatting will prevent you from accidentally executing output in the console. The [1] in the output indicates the output has one line of information.


3.2 R functions

In addition to basic operations, like addition (+), subtraction (-), multiplication (*), division (/), and exponents (^), R has many built in functions that can be used to run calculations and statistical analyses in the console. All functions have the same appearance, some text followed by parentheses, like sqrt() and round():

sqrt(25)
## [1] 5
round(3.1415926535, digits = 4)
## [1] 3.1416


3.3 Order of operations

When entering calculations into the console, remember that the same order of operations will always be applied from left to right (B.E.D.M.A.S.):

  • Brackets ()
  • Exponents ^ and square roots \(√\)
  • Division / and Multiplication *
  • Addition + and Subtraction -


13 + 3 * 7 / sqrt(9)
## [1] 20

In this example, R took the square root of 9 to get 3, multiplied 3 * 7 to get 21, divided 21 by 3 to get 7, and then added 7 to 13 to get the correct answer, 20.


4 Packages


4.1 Built-in packages

The various functions used throughout the tutorials are found in separate program packages, some of which come with RStudio, and some you will need to install on your own. To use any function within a package installed on your machine, the package must also be loaded.

A package must be installed before it can be loaded
A package must be loaded before it can be used

Just as an example, the foreign package comes installed in RStudio, but it is not loaded by default. To see this, click the “Packages” tab in the lower-right panel and scroll to foreign under the System Library packages. Because foreign appears in this list, you know it is installed, but because there is no mark in the check box, you also know it is not loaded. You can load the foreign package either by checking the box, or enter library(foreign) in the console.


4.2 Download and install new packages

All other packages, like psych, do not come installed with RStudio so you will need to install them first. Click the “Install” button in the “Packages” tab to open the “Install Packages” window. Enter the package name, like psych, and click “Install.”


package
Find packages to install in the packages tab


Once installed, you will see the psych package listed under the User Library in the “Packages” tab. To load the package, check the box next to psych in the “Packages” tab or enter library(psych) in the console.

4.3 Packages on personal vs. public computers

When you install a new package on your personal computer, you will not need to install it again after you restart your computer. However, public computers may be setup to clear new data with each restart, so depending on the machine, you may or may not need to reinstall a new package each time the machine restarts.

When you open a new session of RStudio, you will need to load packages that are not automatically loaded, whether you are using your personal computer or public computer. A major advantage of saving your work in RMarkdown (see below) is you can simply list all the required packages to load so you don’t have to remember to complete this step each time you work on a project.


5 RStudio file system

The Files tab in the lower-right panel is a built in file explorer, like the one you already use to find files on your computer. By default, it will show you the files in your R Working Directory.

Tips:

  • Getting in the habit of keeping all of your R-related files in one folder that is set as your working directory makes working in RStudio much easier.
  • You can change your working directory by clicking the “More” drop down box.

files
RStudio file tab


6 RMarkdown

RMarkdown, which is built into RStudio, combines simple text editing with R code execution. This application is useful for creating reports that contain text, graphs, and statistical output (e.g., all the tutorials are made in RMarkdown). It is also one of the best ways to organize and save your work because you can document each step and later rerun your analyses with just a few clicks.


6.1 Open a new RMarkdown file

To open a new RMarkdown (.rmd) file, click File -> New File -> R Markdown. Choose the HTML report format. The new RMarkdown file will open in the upper-left panel of the RStudio window with some formatting, text, and example code chunks already included. Save the .rmd file somewhere in your working directory.


console
RMarkdown panel


In the RMarkdown header, you can edit the yellow text behind title, author, and date.


6.2 RMarkdown code chunks

If you examine the default RMarkdown file, you’ll see shaded areas that begin and end with three accent marks (```); these areas are for code chunks. Following the first set of accent marks, you’ll see some text in brackets, which is a way to name the code chunk and tell RMarkdown how to handle the code chunk when making a report (more on reports below).

The first code chunk below the header is a place for commands that will run, but not be shown in the report. You should delete anything in this first code chunk, but you can add other commands. For example, you can use library() in this space to load the packages needed for your work.

Take a moment to read the default text under the “## R Markdown” heading in the new RMarkdown file. Then click the green play arrow at the far right of the code chunk that contains summary(cars) to automatically add and execute the command in the console. You will also see a preview of the output below the code chunk.

##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00


Next, click the green arrow in the second code chunk that contains plot(pressure) to see a preview of the plot. Note, pressure is also a data set included with RStudio.


Note, cars and pressure are small data sets included with RStudio, so there was not reason to import the data first (see tutorial 1). Many free data sets can also be found in other packages.


6.3 Adding and editing code chunks

To practice adding code chunks in your RMarkdown file, start by duplicating (copy and paste) the entire code chunk that contains summary(cars).

  1. In the new copy of this code chunk, change {r cars} to {r cars2}
  2. Replace the command summary(cars) with describe(cars)
  3. Add library(psych) above describe(cars), alternatively, you can add it to the first code chunk, under knitr::opts_chunk$set(echo = TRUE)
  4. Click the green arrow to execute describe(cars)

console
Editing code chunks in RMarkdown


6.4 Markdown reports

You can generate an .html report from the work in your RMarkdown file by clicking the Knit button at the top of the RMarkdown panel. RStudio will automatically save a copy of the new .html report in the same folder as your RMarkdown file.

Note, you’ve now created two separate files, one RMarkdown file (.rmd) and one report (.html). Only use RStudio and RMarkdown to open and edit .rmd files. Use any web browser to view your .html report (these cannot be edited).

See this guide for many more details on creating RMarkdown reports.


6.5 Saving your tutorial work in RMarkdown

Whether you’re completing a tutorial or working on your own project, saving your work in RMarkdown is highly recommended, just be sure to start a new RMarkdown file for each new project. Although it may initially seem like the extra steps are not worth the effort, saving your work in RMarkdown will make things much easier as you move on to more complex projects.