Learn R Programming

cancensus (version 0.4.8)

get_census: Access to Canadian census data through the CensusMapper API

Description

This function allows convenient access to Canadian census data and boundary files through the CensusMapper API. An API key is required to retrieve data.

Usage

get_census(
  dataset,
  regions,
  level = NA,
  vectors = c(),
  geo_format = NA,
  labels = "detailed",
  use_cache = TRUE,
  quiet = FALSE,
  api_key = Sys.getenv("CM_API_KEY")
)

Arguments

dataset

A CensusMapper dataset identifier.

regions

A named list of census regions to retrieve. Names must be valid census aggregation levels.

level

The census aggregation level to retrieve, defaults to "Regions". One of "Regions", "PR", "CMA", "CD", "CSD", "CT", "DA", "EA" (for 1996), or "DB" (for 2001-2016).

vectors

An R vector containing the CensusMapper variable names of the census variables to download. If no vectors are specified only geographic data will get downloaded.

geo_format

By default is set to NA and appends no geographic information. To include geographic information with census data, specify one of either "sf" to return an sf object (requires the sf package) or "sp" to return a SpatialPolygonsDataFrame-class object (requires the rgdal package).

labels

Set to "detailed" by default, but truncated Census variable names can be selected by setting labels = "short". Use label_vectors(...) to return variable label information in detail.

use_cache

If set to TRUE (the default) data will be read from the local cache if available.

quiet

When TRUE, suppress messages and warnings.

api_key

An API key for the CensusMapper API. Defaults to options() and then the CM_API_KEY environment variable.

Details

For help selecting regions and vectors, see list_census_regions and list_census_vectors, or check out the interactive selection tool at https://censusmapper.ca/api by calling explore_census_vectors()

Examples

Run this code
# NOT RUN {
# Query the API for data on dwellings in Vancouver, at the census subdivision
# level:
# }
# NOT RUN {
census_data <- get_census(dataset='CA16', regions=list(CMA="59933"),
                          vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"),
                          level='CSD')

# Query the API for data on dwellings in Vancouver, at the census subdivision
# level, and return the associated geography files in \code{sf} format:
census_data <- get_census(dataset='CA16', regions=list(CMA="59933"),
                          vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"),
                          level='CSD', geo_format = "sf")

# Make the same query, but this time drop descriptive vector names:
census_data <- get_census(dataset='CA16', regions=list(CMA="59933"),
                          vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"),
                          level='CSD', geo_format = "sf", labels="short")

# Get details for truncated vectors:
label_vectors(census_data)
# }

Run the code above in your browser using DataLab