get_census_api {wru} | R Documentation |
Census API function.
Description
get_census_api
obtains U.S. Census data via the public API.
Usage
get_census_api(
data_url,
key = Sys.getenv("CENSUS_API_KEY"),
var.names,
region,
retry = 0
)
Arguments
data_url |
URL root of the API,
e.g., |
key |
A character string containing a valid Census API key, which can be requested from the U.S. Census API key signup page. By default, attempts to find a census key stored in an
environment variable named |
var.names |
A character vector of variables to get,
e.g., |
region |
Character object specifying which region to obtain data for.
Must contain "for" and possibly "in",
e.g., |
retry |
The number of retries at the census website if network interruption occurs. |
Details
This function obtains U.S. Census data via the public API. User can specify the variables and region(s) for which to obtain data.
Value
If successful, output will be an object of class data.frame
.
If unsuccessful, function prints the URL query that caused the error.
References
Based on code authored by Nicholas Nagle, which is available here.
Examples
## Not run:
get_census_api(
data_url = "https://api.census.gov/data/2020/dec/pl",
var.names = c("P2_005N", "P2_006N", "P2_007N", "P2_008N"), region = "for=county:*&in=state:34"
)
## End(Not run)