library(downloader)
library(sf)
library(data.table)
library(usmap)
library(withr)
library(RcensusPkg)
# Get the generalized tract geometries for Los Alamos, New Mexico
# Determine the fips codes for New Mexico and Los Alamos
nm_los_alamos_fips <- usmap::fips(state = "new mexico", county = "los alamos")
nm_fips <- substr(nm_los_alamos_fips, 1, 2)
los_alamos_fips <- substr(nm_los_alamos_fips, 3, 5)
# Create an expression to filter just the Los Alamos geometries
express <- parse(text = paste0("COUNTYFP == ", '"', los_alamos_fips, '"'))
# Define a temporary output folder for the downloaded shapefiles
output_dir <- withr::local_tempdir()
if(!dir.exists(output_dir)){
dir.create(output_dir)
}
# Get the tract geometries for just Los Alamos
losalamos_tracts_sf <- RcensusPkg::tiger_tracts_sf(
state = nm_fips,
general = TRUE,
express = express,
output_dir = output_dir,
delete_files = FALSE
)
Run the code above in your browser using DataLab