Setup course software

Tutorial
Setup
Installation instructions to install R/RStudio/Git locally.
Modified

January 28, 2025

R

R is an open-source programming language based on the S from the 1970s. It is very popular in the physical and social sciences due to it’s cost (free) and versatility. Thousands of expansion libraries have been published which extend the tasks R can perform, and users can write their own custom functions and/or libraries to perform specific operations.

Installing R

  1. Download the latest binary distribution for your operating system (e.g. Windows, Mac OS X, or Linux) from CRAN, a network of servers around the world which store identical copies of the R binaries, source code, and thousands of additional libraries. The binary version has been pre-compiled and is the easiest to install. This semester we are using R 4.4.1. Windows users should make sure to select the binary for the base distribution.1
  2. Run the installation from the file you just downloaded (.exe or .pkg).
Additional requirements

Individuals using a MacOS device also need to install

Individuals on a Windows computer also need to install RTools in order to compile R packages from source.

  1. Open R. You should see a screen similar to this:

A screenshot of the R console

A screenshot of the R console

This is the default R console. You can use this as your development environment where you write and execute code. However the default R console is very minimalistic and not much more useful than a text editor. This is why we will use RStudio for programming in R. However to demonstrate that your R installation works, type 5 + 2 in the console and press enter. You should see the following:

5 + 2
[1] 7

Congratulations! You just programatically performed basic arithmetic.

RStudio

As previously mentioned, the base R distribution is not the best for developing and writing programs. Instead, we want an integrated development environment (IDE) which will allow us to write and execute code, debug programs, and automate certain tasks. In this course we will use RStudio, perhaps the most popular IDE available for R. Like R, it is open-source, expandable, and provides many useful tools and enhancements over the base R environment.

Installing RStudio

  1. Download the latest version of RStudio appropriate for your operating system. Be sure to select the “Installer”, not the “Zip” files or source code. For Windows users, the file should have a .exe extension; Mac users should see a .dmg extension.
  2. Install it!

Test it

To make sure you’ve done everything correctly, open up RStudio on your computer. You should see something that looks like this:

A screenshot of the RStudio IDE

A screenshot of the RStudio IDE

We’ll discuss this in more detail later, but the RStudio IDE is divided into 4 separate panes (one of which is hidden for now) which all serve specific functions. For now, to make sure R and RStudio are setup correctly, type x <- 5 + 2 into the console pane (the one on the left side of your screen - this is equivalent to the main window you saw when you opened the base R program, where you can type and run live R code) and execute it by pressing Enter/Return. You just created an object in R called x. What does this object contain? Type print(x) into the console and press enter again. Your console should now contain the following output:

x <- 5 + 2
print(x)
[1] 7

Updating R and RStudio

  • If you already installed R or RStudio for a previous course or research, update both to the most current version. Generally this entails downloading and installing the most recent version of both programs. When you update R, you don’t actually remove the old version - you simply have both versions on your computer and default to the most recent version. Sometimes this is useful when specific R libraries require an older version of R, however we will generally stick to the most recent versions of R and RStudio.
  • When you update R, make sure to update your libraries as well. The following command should perform most of this work (note you may have to manually update certain libraries such as those installed directly via Github).
update.packages(ask = FALSE, checkBuilt = TRUE)

Add-on libraries

We will frequently use libraries in R that are not part of the base distribution. To install additional libraries that have been submitted to the CRAN repository, we use the install.packages function in R.2 For instance, one library we will use extensively in this course is {dplyr}, a package for data manipulation. To install {dplyr} , run the following command:

install.packages("dplyr")

If you run into errors later in the course about a function or package not being found, run the install.packages function to make sure the package is actually installed. For example, I might want to use the broom package by David Robinson to tidy the results of my statistical analysis. What happens if I try to use the library without installing it?

library(broom)
## Error in library(broom): there is no package called 'broom'

Okay then, let’s install it (repos tells R from which CRAN server we want to download the package - generally you will not need to specify this when you install packages).

install.packages("broom", repos = "http://cran.rstudio.com")
## 
## The downloaded binary packages are in
##  /var/folders/vw/l7k7vwhn3qqd990ww0dd101c0000gn/T//Rtmp6AWVP6/downloaded_packages

You should install some essential packages now so that you will need early in the course. Copy and paste the following command into the R console to do this:

# install the major packages from the tidyverse
install.packages(c("tidyverse", "renv", "usethis", "gitcreds", "gh"))

Git

Git is a version control system originally created for developers to collaborate on large software projects. Git tracks changes in the project over time so that there is always a comprehensive, structured record of the project. Each project is stored in a repository that includes all files that are part of the project. As social scientists, this is more than just computer scripts - this can include data files and reports, as well as our source code.

Git can be used locally by you on a single computer to track changes in a project. You do not need to be connected to the internet to use Git. However if you want to share your work with a larger audience, the easiest solution is to host the repository on a web site for others to download and inspect. You can host a public (open to the world) or private (open to just you or a few individuals) repository. GitHub has become the largest hoster of Git repositories and includes many useful features beyond the standard Git functions.

Chances are that by now you’ve seen or even used GitHub. Professionally, you should know how to use Git and GitHub to manage projects and share code. In this class, we will use Git and GitHub to share code and distribute/collect assignments.

Install Git

Note

If you have a MacOS device, you likely already have Git installed. There’s also a good chance it is out-of-date, so you should still follow the instructions below to update your installation to the latest version.

Acknowledgments

sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.2 (2024-10-31)
 os       macOS Sonoma 14.6.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2025-01-28
 pandoc   3.4 @ /usr/local/bin/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 cli           3.6.3   2024-06-21 [1] CRAN (R 4.4.0)
 digest        0.6.35  2024-03-11 [1] CRAN (R 4.3.1)
 evaluate      0.24.0  2024-06-10 [1] CRAN (R 4.4.0)
 fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
 here          1.0.1   2020-12-13 [1] CRAN (R 4.3.0)
 htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.3.1)
 htmlwidgets   1.6.4   2023-12-06 [1] CRAN (R 4.3.1)
 jsonlite      1.8.9   2024-09-20 [1] CRAN (R 4.4.1)
 knitr         1.47    2024-05-29 [1] CRAN (R 4.4.0)
 rlang         1.1.4   2024-06-04 [1] CRAN (R 4.3.3)
 rmarkdown     2.27    2024-05-17 [1] CRAN (R 4.4.0)
 rprojroot     2.0.4   2023-11-05 [1] CRAN (R 4.3.1)
 rstudioapi    0.17.0  2024-10-16 [1] CRAN (R 4.4.1)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
 xfun          0.50.5  2025-01-15 [1] https://yihui.r-universe.dev (R 4.4.2)
 yaml          2.3.10  2024-07-26 [1] CRAN (R 4.4.0)

 [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────

Footnotes

  1. Advanced users may want to install from source, but most likely if you are an advanced user you don’t need to take this course.↩︎

  2. “Package” and “library” are two terms many programmers use synonymously. While there are technically differences between the two terms, we will use them interchangably in this course.↩︎