2024 R Workshop Setup

https://machlis.com/Workshop2024/
Auto-advance: https://machlis.com/Workshop2024/setup.html

Sharon Machlis

Software to download and install

Download R software (for either Windows or Mac) and the RStudio desktop application. Links for both at https://posit.co/download/rstudio-desktop/

Screenshot showing links to download R and RStdio

If you can’t/don’t want to get an installation working

If you can’t or don’t want to get R running on your own local system, you can run R and RStudio in the cloud for free. Sign up at https://posit.cloud/plans/free.

This gives you 25 compute usage hours / month, so use wisely.

Additional software to install

These are sometimes needed for certain R package installations locally, better to be safe and avoid problems:

Windows

Rtools, a regular software package. Find, download and install the appropriate Rtools.exe file for your system here: https://cran.r-project.org/bin/windows/Rtools/. You probably need RTools 4.4 if you are installing the current version of R.

OS X

Install Xcode, Apple’s development environment, which should be available in the Mac App Store.

Create a Project

File > New Project

Screenshot showing how to create a new project in RStudio

Select New Project

Name your new directory

See what happens when you close RStudio

DON’T save your workspace if it asks!!!

To prevent it from asking: Tools > Global Options > General > Never for “Save workspace to .RData on exit”

Screenshot showing how to prevent RStudio from asking to save workspace

From A Brief Introduction to RStudio and the rest of sections 2.2 and 2.3

https://www.machlis.com/R4Journalists/a-brief-introduction-to-rstudio.html

It will be helpful to understand how to use the RStudio console, create an R script file, and run R code before the workshop. This way, we can get right to learning R!

Try out a limited R console here ⬇️

Install Packages

Install the 3 packages that are suggested in the book sections plus several more we’ll be using. You can do all that by running the R command

install.packages(c("tidyverse", "rio", "pacman", "quantmod", "dygraphs", "janitor", "DT"))

This works in Post Cloud as well.

Optional

Install the tidycensus, tigris, and mapview packages with install.packages(c("tidycensus", "tigris", "mapview")). Get a US Census Bureau API key at http://api.census.gov/data/key_signup.html

Run this in your console to store your Census API key in R. Leave out the , install = TRUE if you are not on your own local computer.

library(tidycensus)
census_api_key("YOUR API KEY", install = TRUE)

4 More Optional Packages

install.packages(c("esquisse", "scales", "ggeasy", "ggcharts"))

Test to See if You Have Them All

The pacman package’s p_load() function will install a package if it’s not already installed, or load it if it’s already installed. Doesn’t require quotations around the package names or the c() to list them.

library(pacman) # Loads the pacman package
p_load(tidyverse, rio, pacman, quantmod, dygraphs, janitor, DT, tidycensus, tigris, mapview, esquisse, scales, 
       ggeasy, ggcharts)

Optional: Download Data Files in Advance

If we’re worried about Internet in the classroom, you can download the data files to your project directory in advance:

download.file("https://raw.githubusercontent.com/smach/SampleData/refs/heads/main/states.csv",
              destfile = "states.csv"
              )

download.file("https://www.machlis.com/Workshop2024/us_presidential_2parties_2016_2020.csv",
              destfile = "election_data.csv"
              )

download.file('https://github.com/sfirke/janitor/raw/refs/heads/main/dirty_data.xlsx', "dirty_data.xlsx", mode = "wb")

Optional

If you have time and want to get ahead, we’ll be going through Chapter 3 sections 3.1 to 3.3 in the workshop.