3.2 Downloading and installing R

The first step to get R onto your machine is to go to the website, download the correct version of R, and install.

The website is https://www.r-project.org/.

3.2.1 Choosing a Mirror

Since R is open source, there are many different servers around the world where you can download it. You are welcome to choose any mirror you wish, but you may want to be sure that you know the national language of whichever country you select. I was boring and simply chose a mirror in Pittsburgh because it was closest to my location.

3.2.2 Download and install the correct version

R is available for PCs, Macs, and Linux systems. You will most likely want one of the first two options. Be sure to choose the option in the top box that offers Precompiled binary distributions.

For Macs:

  • Click on the “Download R for (Mac) OS X” link at the top of the page.

  • Click on the file containing the latest version of R under “Files.”

  • Save the .pkg file, double-click it to open, and follow the installation instructions.

For PCs:

  • Click on the “Download R for Windows” link at the top of the page.

  • Click on the “install R for the first time” link at the top of the page.

  • Click “Download R for Windows” and save the executable file somewhere on your computer. Run the .exe file and follow the installation instructions.

Once this is complete, you will never need to actually open R. We will using RStudio to communicate with R.

3.2.3 Downloading and installing RStudio

While R is open source, RStudio is a company that sells versions of its IDE. In short, it is an easy-to-use interface that makes working with R easier. We will be using the free version of RStudio Desktop which is available here:

https://rstudio.com/products/rstudio/download/

For Macs:

  • Click on “Download RStudio Desktop.”

  • Click on the version recommended for your system, or the latest Mac version, save the .dmg file on your computer

  • Double-click it to open, and then drag and drop it to your applications folder.

For PCs:

  • Click on “Download RStudio Desktop.”

  • Click on the version recommended for your system, or the latest Windows version, and save the executable file.

  • Run the .exe file and follow the installation instructions.

3.2.4 Taking Stock

If done correctly, you should now be able to open Rstudio and see a screen that looks like this:

Welcome to R!

Figure 3.1: Welcome to R!

The window on the left is your Console which is exactly what you would see if you opened up R instead of Rstudio.2 The window on the upper-right is your Global Environment. It will show you all of the data sets, variables, and result objects that are currently in R and available to you. Note that it is currently empty because we haven’t done anything yet. The window on your bottom-right has several useful tabs that let us look at our folder directory (as shown) as well as any figures we generate and R packages at our disposal.

This is the default mode of Rstudio. You can input commands into the console right at the “>” and R will execute them line by line. This is fine if you wish to execute one single command, but it becomes tedious if we have a series of commands we need to execute before we arrive at our desired result. We can therefore alter this default mode by adding R-scripts.

An R-script!

Figure 3.2: An R-script!

Clicking on the green plus in the upper left of the screen will give you the option of opening an R-script. An R-script window will now appear and take up half of what was once our console space. An R-script is really nothing more than a text file. We can type several commands in sequence without running them line by line (which we would need to do if we typed them into the console). Once the commands are typed out, we can highlight them all and hit that run button on top. The commands get sent to the console and you’re off…

Running Commands from R-scripts!

Figure 3.3: Running Commands from R-scripts!

The picture above is just a quick example of what an R-script can do. Line 3 tells R to plot all of the variables in a dataset called mtcars. Highlighting that line and hitting the run button sends the command to the console below, and the plot figure shows up in the Plots window. That’s that!

3.2.5 Installing Packages

R is essentially a super-powered calculator. In order for it to be able to do some of the sophisticated things we will be doing in the course, we need to install source code called packages.

Whenever you need a package, all you need to do is type:

install.packages("name of package")

Once this is done, the package is installed in your version of R and you will never need to install it again. You will need to unpack the packages each time you want to use them by calling a library command, but we will get to that later.

The first R-script you will run as part of your first assignment is called Install_Packages.R. The executable portion of the code looks like this:

install.packages( c("AER", "car", "dplyr", 
"fastDummies", "readxl", "xtable", "vars",
"WDI", "xts", "zoo", "wooldridge") )

This is a simple command that asks R to download 11 packages and install them. Download and import the R-script, highlight the portion above, and hit the Run tab at the top of the upper-left window of RStudio. A bunch of notifications will appear on the R console (lower-left window) while the list of packages will be downloaded from the mirror site you selected earlier and installed. This can take some time (about 30 mins) depending on your internet connection, so it is advised to do this when you can leave your computer alone for awhile.


  1. Note that we will never open R by itself, because it is easier to communicate with R through Rstudio.↩︎