This and other labs in this class are based on labs originally developed by C.J.Gabbe and Iris Stewart Frey

Steps in today’s lab

  1. Create a new RScript
  2. Install packages
  3. Load packages
  4. Set your working directory
  5. Load and browse a csv file
  6. Write a comment explaining what you did
  7. Save file as RData format
  8. Remove files from the environment
  9. Load RData file
  10. subset data based on a parameter
  11. Plot a shapefile in R

Getting started: Create a ‘Module1_Lab’ folder on your computer (preferably in Google Drive, Dropbox or elsewhere that’s backed up to the cloud)

1. Create a new RScript

An RScript is simply a plain text file that you can use to write and save your code. One of the biggest advantages of using an RScript, instead of just entering line by line in the console is that you can create a record of what you’ve done, replicate and update your work later, and share your work with others.

You create a new RScript by going to File > New File > RScript. Give it a name (i.e. “ces.R” for Cal Enviroscreen or “lab1.R”) and save the RScript to your ‘Module1_Lab’ folder.

2. Install packages

Packages are extensions of R. You can think of them as apps for the software. You’ll only need to install each R package once. Let’s start by installing the tidyverse package. First try:

#install.packages("tidyverse")
#install.packages("ggplot")
#install.packages("sf")
#intall.packages("tmap")
library(sf)

In the unlikely event that the code above doesn’t work, you may need to designate the CRAN repository:

install.packages("tidyverse", repos = "http://cran.us.r-project.org")
install.packages("sf")

The sf package ### 3. Load packages

Every time you start a new R session, you’ll need to re-load any packages you’re going to use during the session. You load packages using the library call.

library("tidyverse")
library("sf")

4. Set your working directory

The working directory points to where you will be loading and saving files. This will save you time and make it easier to others to replicate your work. We’ll start by setting our working directories to the folder where you’ll be working from on this project. Here are two examples (change to the folder path that’s applicable to you):

Example 1:
setwd("/Users/johndialesandro/Library/CloudStorage/GoogleDrive-jdialesandro@scu.edu/My Drive/ENVS117/Labs")

You can check your working directory by typing getwd() into the console.

5. Load and browse a csv file

Download the state of California’s csv file of [mobile home parks]file from camino and place it in your working directory aka move the file into your Module1_Lab folder.

CA_MHPs <- read_csv("HCD MH Park Query 2.28.2018.csv")

You can also read a csv file directly from the internet. Let’s say we want to download data from San Jose Open Data Portal on parks in San Jose. Once you have the URL, you can pull the data directly into R. Enable or suppress messages on column types by using show_col_types = TRUE or FALSE

SanJoseParks <-read_csv("https://gisdata-csj.opendata.arcgis.com/datasets/CSJ::park.csv?where=1=1&outSR=%7B%22latestWkid%22%3A2227%2C%22wkid%22%3A102643%7D", show_col_types = FALSE)

Where do you think I got the URL for the download in the code above?

The San Jose Open Data Portal is an extensive GIS data repository you can keep explore for your final project. Another great link is the The California Natural Resources Agency Open Data is an extensive environmental data repository you can keep explore for your final project

6. Write a comment explaining what you did

You can write comments – to your future self and to others – by using # marks. Any line that starts with a # will be a text comment. For example:

# Don't forget to load packages before you need them
library("tidyverse")
library(sf)

We can also use the sf package which stands for simple feature to plot a GIS file such as a shapefile. Download the [SFSlowStreetsData]file from camino and place it in your working directory aka move the file into your Module1_Lab folder. Read in the SlowStreets.shp file with the read_sf package

SlowStreets <- read_sf("SlowStreets.shp") 
ggplot(SlowStreets)+
    geom_sf()

You can also explore some demo datatsets that the sf package has such as a spatial feature of the state of North Carolina.

demo(nc, ask = FALSE, echo = FALSE)
plot(st_geometry(nc))

You can also use the sf package to read in a familar shapefile you may have used with me in ENVS 116 the calenviorscreen shapefile. You can download the shapefile file from camino and place it in your working directory aka move the file into your Module1_Lab folder.

CES <- read_sf("CES4 Final Shapefile.shp")
plot(st_geometry(CES))

We can view the data avaiable in our attribute table just like you can in ArcGIS Pro. We have a function called glimpse that will show table “wide ways” and a function called head and a function called tail to show either the data’s top 6 rows and the bottom 6 rows for tail.

glimpse(CES)
head(CES)

We can plot a map using one variable og interest such as CIscore

plot(CES["Hispanic"])

But notice that the data spread is hard to visualize as it is does not a color scheme that does not appropriately show the CISCORE but we can add a breaks argument to divide up the data

plot(CES["CIscore"], breaks = c(0,25,50,75,100))

You may also remember from ENVS 116 that we have a set of classification schemes for legends such as equal interval, qunatile or Jenks natural breaks

plot(CES["CIscore"], breaks = "quantile")

Notice that are legend is odd now becasue there are negative values that lengthen the legend. This is because we have no data values that are coded as -9999. We can subset the data to remove these which we will learn later on

library("tidyverse") # Tidyverse is a must-have package

7. Save file as RData format

Save your first csv file; notice the .RData extension:

save(CA_MHPs, file = "HCD_MH_Park_Query_2.28.2018.RData") 

What does “CA_MHPs” refer to?

Now try saving the Excel file with San Jose Parks to the same folder as an RData file.

8. Remove files from the environment

Sometimes you want to clear your environment. There are a few ways to do this:

  1. You can clear everything using the little broom icon.
  2. You can list the objects you want to clear (and separate multiple objects with a comma):
rm(CA_MHPs)
  1. You can clear everything:
rm(list=ls())

9. Load RData file

Let’s try reloading the mobile home park data back into our environment:

load("HCD_MH_Park_Query_2.28.2018.RData") 

Applying what you’ve learned

Save the RScript you’ve been working with. Edit and add to your RScript to do the following:

  1. Install the tmap package (we’re going to use tidyverse too, but you already installed it earlier).
  2. Load the “tmap” package (you should already have code to load tidyverse)
  3. Import the csv with California Disadvanatged Communties from the state’s open data site. Write the code to import it from your Z drive and directly from the internet.
  4. Add a comment explaining a sentence about the contents of the csv
  5. Save the csv into RData format in your Module1_Lab folder.
  6. Remove the file from Environment.
  7. Load the RData version of your file back into the Environment.
  8. Save your RScript to your Module1_Lab folder.
  9. read CES file that we used earlier into R as an Sf feature and plot it in R
  10. plot the variable PovertyP and another variable of your choice.