get_census_api: Census API function.

View source: R/get_census_api.R

get_census_apiR Documentation

Census API function.

Description

get_census_api obtains U.S. Census data via the public API.

Usage

get_census_api(data_url, key, var.names, region, retry = 0)

Arguments

data_url

URL root of the API, including the question mark, e.g., "https://api.census.gov/data/2010/dec/dec/sf1?".

key

A required character object containing user's Census API key, which can be requested here.

var.names

A character vector of variables to get, e.g., c("P005003","P005004","P005005", "P005006"). If there are more than 50 variables, then function will automatically split variables into separate queries.

region

Character object specifying which region to obtain data for. Must contain "for" and possibly "in", e.g., "for=block:1213&in=state:47+county:015+tract:*".

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/2010/dec/sf1?", key = "...",
  var.names = c("P005003", "P005004", "P005005", "P005006"), region = "for=county:*&in=state:34"
)

## End(Not run)


wru documentation built on Oct. 22, 2022, 1:05 a.m.