get_rtigris: Retrieve and cache data from the tigris package.

View source: R/get_rtigrs.R

get_rtigrisR Documentation

Retrieve and cache data from the tigris package.

Description

This function retrieves data from the 'tigris' package, caches the results as an RDS file, and returns the data. It avoids redundant downloads by checking for the existence of a cached file before attempting to download.

Usage

get_rtigris(tigris_fun, save_dir = here::here("data-raw/rtigris/"), ...)

Arguments

tigris_fun

A function from the 'tigris' package. This should be passed as a bare function name (e.g., 'zctas', not '"zctas"').

save_dir

The directory where the cached RDS files should be saved. Defaults to 'here::here("data-raw/tigris/")'.

...

Additional arguments to be passed to the 'tigris_fun'. These arguments will be incorporated into the name of the cached file, allowing for different cached versions based on different function arguments.

Details

This function simplifies the process of working with the 'tigris' package by automatically caching downloaded data. It constructs a file name based on the function called and its arguments, ensuring that different datasets or different queries to the same dataset are stored separately. This prevents unnecessary downloads and speeds up subsequent access to the same data.

The function performs several checks to ensure that the provided 'tigris_fun' is a valid function from the 'tigris' package and that the returned object is an sf object with a geometry column.

Value

An sf object containing the data retrieved from the specified 'tigris_fun'.

Examples

## Not run: 
# Get ZCTAs for California and cache the result.
ca_zctas <- get_rtigris(zctas, state = "CA")

# Get ZCTAs for California with a different year and cache the result separately.
ca_zctas_2020 <- get_rtigris(zctas, state = "CA", year = 2020)

# Get counties for North Carolina.
nc_counties <- get_rtigris(counties, state = "NC")

## End(Not run)


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