Getting Started with R

Install R and RStudio

You should install both R and RStudio on your computer:

  1. Download R.
  2. Download RStudio Desktop. RStudio provides a nice interface for working with R.

If you already have R and RStudio on your computer, then you should make sure they are up to date. You can check your version of R by typing version into the R console.

Open an R Script

Open RStudio. Start a new R Script by clicking the green + in the upper left corner, just below ‘File’. Select R Script from the dropdown menu. This is where you will compose your code. (An R Script is the equivalent of a Stata DO File.)

The Console window is where you will see the results of your code after you run it. The Environment window is where you will see datasets and other data objects you will create. The Plots window is where your maps will display. Also note that there is a Help window, where you can search for help when writing your code.

Set Your Working Directory

The working directory is where you will store the files you are working with. By setting the working directory, you avoid having to type the full path out every time you want to load or save a file.

  1. Create a new folder called Spatial Workshop on your Desktop. This will be your working directory, where you’ll store all of the files for this workshop.

  2. Determine the address of your working directory: Open your Spatial Workshop folder on your Desktop and right-click on the file path, then select Copy address.

  3. Paste the address you copied into your R Script (Ctrl+V). It will likely appear in this format: C:\Users\Carrie\Desktop\Spatial Workshop

  4. Remove the C: and change the forward slashes (\) to back slashes (/). Put quotes (" ") around everything. Then type setwd( before it, and enclose with another ) after. It should look like this (“Carrie” should be replaced with your directory):

# Set Working Directory
setwd("/Users/Carrie/Desktop/Spatial Workshop")

Save Your R Script

To save your R Script, click File then Save As. Name your script “R Spatial Tutorial”. Save your script in your Spatial Workshop folder.

Download Files

You will need to download the following shapefiles from the internet for this workshop:

Each download will come with a zipped folder of several files. Extract the files from each downloaded folder into your Spatial Workshop folder.

You will also need the following files:

Save all of these files in your Spatial Workshop folder.

Install Packages

R is an open-source language, which means that anyone can access the language code and write new code to continue developing the language. Users can contribute “packages” for others to use, which are useful collections of functions, data, and code. Packages are downloaded from the internet, using some simple code within R. You will need a decent network connection to download packages.

Here is a list of the all packages we will use in this tutorial. I recommend installing them in advance of our workshop because they may take some time to download if the network connection is poor.

Paste the following code into your R Script, highlight all of the code, and press Ctr + Enter on your keyboard.

# Install packages
install.packages("sp") #provides classes & methods for spatial data
install.packages("rgdal") #for working with shapefiles
install.packages("prettymapr") #for adding a scale bar and north arrow
install.packages("foreign") #for importing DBF files (or Stata, SAS, etc) 
install.packages("readxl") #for importing Excel files
install.packages("ggmap") #for geocoding and advanced spatial visualization
install.packages("classInt") #for creating categories of data
install.packages("RColorBrewer") #for pretty color palettes
install.packages("dplyr") #for manipulating data
install.packages("leaflet") #for interactive maps
install.packages("htmlwidgets") #for saving leaflet maps as html files

Once you have installed a package, it will remain on your computer. You do NOT need to install it again the next time you use R.

General Tips for Using R

  1. Write your code in a script (similar to a Stata DO File) so you can easily edit and save it.
  2. To run a portion of your script, highlight it and press Ctrl + Enter at the same time.
  3. “Comment” (take notes) in your script using the # symbol (equivalent to using the * symbol in Stata). Imagine someone (like yourself in 5 years) opening this file and trying to understand what you were working on.
  4. Keep your files well organized and give them meaningful names.