get_rnhgis_shp: Download and cache NHGIS shapefiles.

View source: R/get_rnhgis_shp.R

get_rnhgis_shpR Documentation

Download and cache NHGIS shapefiles.

Description

This function downloads shapefiles from the NHGIS (National Historical Geographic Information System) using the 'ipumsr' package. It caches downloaded shapefiles as RDS files to avoid redundant downloads. It requires an IPUMS API key to be set as an environment variable named 'IPUMS_API_KEY'.

Usage

get_rnhgis_shp(
  shp,
  refresh = FALSE,
  save_dir = here::here("data-raw/rnhgis_shp/")
)

Arguments

shp

A character vector of length one specifying the name of the shapefile to download. This name corresponds to the shapefile codes used by NHGIS (e.g., "us_state_2020_tl2020"). See the 'ipumsr' documentation for how to find these codes.

refresh

Logical. If 'TRUE', forces a refresh of the cached; if 'FALSE' (default), uses the cached data.

save_dir

The directory where the downloaded and processed shapefiles will be saved as RDS files. Defaults to '"data-raw/rnhgis_shp/"' using ‘here::here()'. This directory will be created if it doesn’t exist.

Value

An 'sf' object containing the downloaded shapefile.

Examples

## Not run: 
# Download and cache the 2020 US counties shapefile.
states <- get_rnhgis_shp("us_state_2020_tl2020")

# Subsequent calls with the same 'shp' argument will load the cached
# shapefile.
states_cached <- get_rnhgis_shp("counties")
identical(states, states_cached) # TRUE

## End(Not run)


ChandlerLutz/CLmisc documentation built on Feb. 28, 2025, 10:05 p.m.