This code through explores how to take the functions and data
wrangling steps from the labs and put them in a utilities.R
file.
Specifically, we’ll explain and demonstrate how to create the
utilities file then how to import the functions and data
from the file into the RMD document for further analysis. The data and
functions housed in the .R file will be used to create the
San Diego Dorling Cartograms.
This topic is valuable because it will help keep the R Markdown file clean and easier to read/interpret. This also allows for portability of the functions and data. Anyone can download and import steps from the utilities file into the R Markdown platform to utilize. This method allows the user to show only the most important data steps rather than all of the background processing.
utilities.R file
Here, we’ll show how to extract information from the various labs and
compile it into one .R file.
The code below can all be taken out of the Markdown documents and
placed into one utilities.R file. It should be a
.R file, not .RMD. It includes packages, functions, and
data processing steps to get the final data sets needed to create the
maps.
# library( dplyr )
# library( here )
# library(import)
# library( knitr )
# library( pander )
# library(tidyverse)
# library(tidycensus)
# library(cartogram)
# library(tmap)
# library( stargazer )
# library( scales )
# library( geojsonio )
# library( sp )
# library( sf )
# library( mclust )
# library( ggplot2 )
# library( ggthemes )
#obtain_crosswalk = function() {
# URL <- "https://raw.githubusercontent.com/DS4PS/cpp-529-master/master/data/cbsatocountycrosswalk.csv"
# cw <- read.csv( URL, colClasses="character" )
# cw$urban <- ifelse( cw$cbsaname == "", "rural", "urban" )
# keep.these <- c( "countyname","state","fipscounty",
# "msa","msaname",
# "cbsa","cbsaname",
# "urban" )
# cw <- dplyr::select( cw, all_of(keep.these))
# cw <- cw[ ! duplicated(cw$fipscounty) , ]
# return(cw)}
#crosswalk <- obtain_crosswalk()
# grep( "^SAN", crosswalk$msaname, value=TRUE )
# these.sd <- crosswalk$msaname == "SAN DIEGO, CA"
# these.fips <- crosswalk$fipscounty[ these.sd ]
# these.fips <- na.omit( these.fips )
# state.fips <- substr( these.fips, 1, 2 )
# county.fips <- substr( these.fips, 3, 5 )
# cbind( these.fips, state.fips, county.fips )
# sd.pop1 <-
# get_acs( geography = "tract", variables = "B01003_001",
# state = "06", county = county.fips[state.fips=="06"], geometry = TRUE ) %>%
# select( GEOID, estimate ) %>%
# rename( POP=estimate )
# sd.pop1$GEOID<-sub( ".","", sd.pop1$GEOID )
# URL <- "https://github.com/DS4PS/cpp-529-master/raw/master/data/ltdb_std_2010_sample.rds"
# census.dat <- readRDS(gzcon(url( URL )))
# sd <- merge( sd.pop1, census.dat, by.x="GEOID", by.y="tractid" )
# sd <- sd[ ! st_is_empty( sd ) , ]
# data.dictionary <-
# structure(list(LABEL = c("tractid", "pnhwht12", "pnhblk12", "phisp12",
# "pntv12", "pfb12", "polang12", "phs12", "pcol12", "punemp12",
# "pflabf12", "pprof12", "pmanuf12", "pvet12", "psemp12", "hinc12",
# "incpc12", "ppov12", "pown12", "pvac12", "pmulti12", "mrent12",
# "mhmval12", "p30old12", "p10yrs12", "p18und12", "p60up12", "p75up12",
# "pmar12", "pwds12", "pfhh12"), VARIABLE = c("GEOID", "Percent white, non-Hispanic",
# "Percent black, non-Hispanic", "Percent Hispanic", "Percent Native American race",
# "Percent foreign born", "Percent speaking other language at home, age 5 plus",
# "Percent with high school degree or less", "Percent with 4-year college degree or more",
# "Percent unemployed", "Percent female labor force participation",
# "Percent professional employees", "Percent manufacturing employees",
# "Percent veteran", "Percent self-employed", "Median HH income, total",
# "Per capita income", "Percent in poverty, total", "Percent owner-occupied units",
# "Percent vacant units", "Percent multi-family units", "Median rent",
# "Median home value", "Percent structures more than 30 years old",
# "Percent HH in neighborhood 10 years or less", "Percent 17 and under, total",
# "Percent 60 and older, total", "Percent 75 and older, total",
# "Percent currently married, not separated", "Percent widowed, divorced and separated",
# "Percent female-headed families with children")), class = "data.frame", row.names = c(NA,
# -31L))
# sd.sp <- as_Spatial( sd )
# sd.sp <- spTransform( sd.sp, CRS("+init=epsg:3395"))
# sd.sp <- sd.sp[ sd.sp$POP != 0 & (! is.na( sd.sp$POP )) , ]
# sd.sp$pop.w <- sd.sp$POP / 9000 # max(sd.sp$POP)
# sd_dorling <- cartogram_dorling( x=sd.sp, weight="pop.w", k=0.05 )
# sd_dorling <- cartogram_dorling( x=sd.sp, weight="pop.w", k=0.05 )
In this step, all of the necessary packages will be loaded and we will determine the root folder for the here() package.
library( dplyr )
library( here )
library(import)
library( knitr )
library( pander )
library(tidyverse)
library(tidycensus)
library(cartogram)
library(tmap)
library( stargazer )
library( scales )
library( geojsonio )
library( sp )
library( sf )
library( mclust )
library( ggplot2 )
library( ggthemes )R Studio tells us that the root folder is /Users/drkristen/Desktop/ASU/PAF 516 CA. The Console shows where here() starts right after the package is loaded. If you need to recall where the starting point is for here() you can simply run the code:
utilities.R fileIn this step, all of the data sets and functions will be imported into the RMD document for further analysis.
This will import anything that you need from the
utilities.R file into the RMD environment to use for
analysis. This includes functions that you will need and data sets. Be
sure to change the .from section to the correct pathway on your computer
to your utilities.R file.
set.seed( 1234 )
import::here("census.dat",
"data.dictionary",
"sd",
"sd.pop1",
"sd.sp",
"crosswalk",
"sd_dorling",
.from = here::here("CodeThroughutilities.R"),
.character_only = TRUE)In this step, all of the data sets and functions will be imported into the RMD document for further analysis.
The remaining lab steps can be completed like normal using the imported data sets.
bb <- st_bbox( c( xmin = -13065349, xmax = -12997344,
ymax = 3820011, ymin = 3920249 ),
crs = st_crs("+init=epsg:3395"))
tm_shape( sd_dorling, bbox=bb ) +
tm_polygons( col="hinc12", n=10, style="quantile", palette="Set1" ) +
tm_layout( "Dorling Cartogram", title.position=c("right","top") )Learn more about the here() package with the following: