Setting up R and Rstudio

The goal of this course is to apply what you learn in the introduction to quantitative methods’ course by learning programming. This involves giving instructions to your computer to perform operations on quantitative data using code. The course focuses on coding with the programming language R, using the software RStudio.

To get the first session off to a good start and avoid wasting too much time setting up, you must bring a laptop with R and Rstudio installed. Instructions are provided below. This should be fairly quick. If you have any issues to install these programs correctly, please contact me before the first class.

R and RStudio are two different things. R is a programming language and software widely used for statistical analysis. While it is possible to use the default software directly, many (if not most) prefer to use RStudio instead. RStudio is an integrated development environment (IDE) that provides a much more functional and user-friendly interface to interact with R, which is why we choose to use it. However, we still need to install R before installing RStudio, as the latter relies on the former to function properly.

Install R

You can install R from CRAN (The comprehensive R Archive Network). On CRAN, you will find links to download the version of R you need for your operating system (Windows, Mac or Linux). Once the download is complete, you need to execute the installer.

Install Rstudio

Once R is set up, you can install Rstudio. For this, go on this webpage, download Rstudio and follow the instructions.

Set preferences and check if everything works

Once the installation of both R and Rstudio is complete, you should open Rstudio. If everything worked, something very similar to the screenshot below should appear. If it doesn’t open, restart your latptop and try again.

RStudio has some default settings that are worth changing to adopt best practices. Please do the following:

  • Tools /Global options/RGeneral : Save workspace to never and uncheck the box Restore .Rdata into workspace at startup.

While R provides a series of basic commands for data manipulation, many of the functionalities we will use come from packages that need to be installed and loaded via RStudio. The most well-known package we will use is called tidyverse. To check if your installation is successful and you are ready to start the course, copy-paste the following code into the console pane and press enter. It might take a few minutes, and at the end, you should see the same message as the one below.

if (!require(tidyverse)) {install.packages("tidyverse")}
Loading required package: tidyverse
── Attaching core tidyverse packages ─────────────────── tidyverse 2.0.0.9000 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Installation issues

If you do not see the message above, welcome in the world of troubleshooting! Depending on your operating system (MacOS, Windows, Linux) and the configuration of your computer, the installation process may not go as smoothly as expected. Here are some common issues and how to solve them:

Mac

If a message appears saying that the package cannot be found, try the following :

if (!require(tidyverse)) {devtools::install_github("hadley/tidyverse")}

Windows

If a message appears saying that the package cannot be found, try to install Rtools by copying and pasting the following code into the console pane.

if (Sys.info()["sysname"] == "Windows") {
  install.packages("Rtools")
}

If a message appears that Rtools is not available, download it from here. Then restart your computer and try to install the tidyverse package again.

If all of this does not work or you have different types of issues, please contact me.