5 + 2
[1] 7
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.
.exe
or .pkg
).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.
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.
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.
.exe
extension; Mac users should see a .dmg
extension.To make sure you’ve done everything correctly, open up RStudio on your computer. You should see something that looks like this:
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
update.packages(ask = FALSE, checkBuilt = TRUE)
If you are using a MacOS device, you will need to install the XCode Command Line Tools. This is a set of tools that allow you to compile and install software on your Mac (in this case, R packages). You can install the tools by running the following command in the terminal:2
xcode-select --install
A dialog box will appear asking you to confirm the installation.
Click “Install” and follow the instructions.
Once the installation is complete, you can close the terminal window.
2 You can access the terminal directly through your Mac using Spotlight. Alternatively, RStudio includes a built-in terminal in the bottom left pane. Just switch from “Console” to “Terminal”
3 “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.
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.3 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?
## 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 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.
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.
sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.4.1 (2024-06-14)
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 2024-10-08
pandoc 3.4 @ /usr/local/bin/ (via rmarkdown)
─ Packages ───────────────────────────────────────────────────────────────────
! package * version date (UTC) lib source
cli 3.6.3 2024-06-21 [1] RSPM (R 4.4.0)
P digest 0.6.35 2024-03-11 [?] CRAN (R 4.3.1)
P evaluate 0.24.0 2024-06-10 [?] CRAN (R 4.4.0)
P fastmap 1.2.0 2024-05-15 [?] CRAN (R 4.4.0)
P here 1.0.1 2020-12-13 [?] CRAN (R 4.3.0)
P htmltools 0.5.8.1 2024-04-04 [?] CRAN (R 4.3.1)
P htmlwidgets 1.6.4 2023-12-06 [?] CRAN (R 4.3.1)
P jsonlite 1.8.8 2023-12-04 [?] CRAN (R 4.3.1)
P knitr 1.47 2024-05-29 [?] CRAN (R 4.4.0)
renv 1.0.7 2024-04-11 [1] CRAN (R 4.4.0)
P rlang 1.1.4 2024-06-04 [?] CRAN (R 4.3.3)
P rmarkdown 2.27 2024-05-17 [?] CRAN (R 4.4.0)
P rprojroot 2.0.4 2023-11-05 [?] CRAN (R 4.3.1)
P sessioninfo 1.2.2 2021-12-06 [?] CRAN (R 4.3.0)
P xfun 0.45 2024-06-16 [?] CRAN (R 4.4.0)
P yaml 2.3.8 2023-12-11 [?] CRAN (R 4.3.1)
[1] /Users/soltoffbc/Projects/info-5001/course-site/renv/library/macos/R-4.4/aarch64-apple-darwin20
[2] /Users/soltoffbc/Library/Caches/org.R-project.R/R/renv/sandbox/macos/R-4.4/aarch64-apple-darwin20/f7156815
P ── Loaded and on-disk path mismatch.
──────────────────────────────────────────────────────────────────────────────