Title: | Access, Retrieve, and Work with Canadian Census Data and Geography |
---|---|
Description: | Integrated, convenient, and uniform access to Canadian Census data and geography retrieved using the 'CensusMapper' API. This package produces analysis-ready tidy data frames and spatial data in multiple formats, as well as convenience functions for working with Census variables, variable hierarchies, and region selection. API keys are freely available with free registration at <https://censusmapper.ca/api>. Census data and boundary geometries are reproduced and distributed on an "as is" basis with the permission of Statistics Canada (Statistics Canada 2001; 2006; 2011; 2016; 2021). |
Authors: | Jens von Bergmann [aut] (API creator and maintainer), Dmitry Shkolnik [aut, cre] (Package maintainer, responsible for correspondence), Aaron Jacobs [aut] |
Maintainer: | Dmitry Shkolnik <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.7 |
Built: | 2024-11-06 04:28:52 UTC |
Source: | https://github.com/mountainmath/cancensus |
Names of municipalities are not always unique, especially at the CSD level. This function takes as input a subset of a regions list as generated from 'list_census_regions()' and de-duplicates names as needed by adding the municipal status in parenthesis. If this does not de-duplicate the name then the geographic identifier will be further added in parenthesis behind that.
add_unique_names_to_region_list(region_list)
add_unique_names_to_region_list(region_list)
region_list |
a subset of a regions list as gotten from 'list_census_regions()' |
The same list of regions with an extra column 'Name' with de-duplicated names.
## Not run: # This will return a warning that no match was found, but will suggest similar named regions. library(dplyr) list_census_regions("CA21") %>% filter(level=="CSD", CMA_UID=="59933") %>% add_unique_names_to_region_list() ## End(Not run)
## Not run: # This will return a warning that no match was found, but will suggest similar named regions. library(dplyr) list_census_regions("CA21") %>% filter(level=="CSD", CMA_UID=="59933") %>% add_unique_names_to_region_list() ## End(Not run)
list_census_regions
to a list suitable for passing to
get_census
.Convert a (suitably filtered) data frame from
list_census_regions
to a list suitable for passing to
get_census
.
as_census_region_list(tbl)
as_census_region_list(tbl)
tbl |
A data frame, suitably filtered, as returned by
|
## Not run: library(dplyr, warn.conflicts = FALSE) # Query the CensusMapper API for the total occupied dwellings # of 20 random Census Subdivisions, in Census 2016. regions <- list_census_regions("CA16") %>% filter(level == "CSD") %>% sample_n(20) %>% as_census_region_list() occupied <- get_census("CA16", regions = regions, vectors = c("v_CA16_408"), level = "Regions") ## End(Not run)
## Not run: library(dplyr, warn.conflicts = FALSE) # Query the CensusMapper API for the total occupied dwellings # of 20 random Census Subdivisions, in Census 2016. regions <- list_census_regions("CA16") %>% filter(level == "CSD") %>% sample_n(20) %>% as_census_region_list() occupied <- get_census("CA16", regions = regions, vectors = c("v_CA16_408"), level = "Regions") ## End(Not run)
list_census_vectors
, search_census_vectors
, find_census_vectors
, or a direct string reference to the vector code.List all child variables from vector hierarchies given either a list of Census
variables returned by
list_census_vectors
, search_census_vectors
, find_census_vectors
, or a direct string reference to the vector code.
child_census_vectors( vector_list, leaves_only = FALSE, max_level = NA, keep_parent = FALSE )
child_census_vectors( vector_list, leaves_only = FALSE, max_level = NA, keep_parent = FALSE )
vector_list |
the list of vectors to be used, either a character vector or a filtered tibble
as returned from |
leaves_only |
boolean flag to indicate if only final leaf vectors should be returned, i.e. terminal vectors that themselves do not have children. |
max_level |
optional, maximum depth to look for child vectors. Default is |
keep_parent |
optional, also return parent vector in list of results. Default is set to |
# Query parent vectors directly using vector identifier child_census_vectors("v_CA16_2510") ## Not run: # Example using multiple vectors coerced into a list child_census_vectors(c("v_CA16_2510","v_CA16_2511","v_CA16_2512")) # or, equivalently selected_vectors <- c("v_CA16_2510","v_CA16_2511","v_CA16_2512") child_census_vectors(selected_vectors) # Example using dplyr and piped arguments library(dplyr, warn.conflicts = FALSE) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2510") %>% child_census_vectors(TRUE) # this will return the equivalent of c("v_CA16_2510", child_census_vectors("v_CA16_2510")) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2510") %>% child_census_vectors(TRUE, keep_parent = TRUE) ## End(Not run)
# Query parent vectors directly using vector identifier child_census_vectors("v_CA16_2510") ## Not run: # Example using multiple vectors coerced into a list child_census_vectors(c("v_CA16_2510","v_CA16_2511","v_CA16_2512")) # or, equivalently selected_vectors <- c("v_CA16_2510","v_CA16_2511","v_CA16_2512") child_census_vectors(selected_vectors) # Example using dplyr and piped arguments library(dplyr, warn.conflicts = FALSE) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2510") %>% child_census_vectors(TRUE) # this will return the equivalent of c("v_CA16_2510", child_census_vectors("v_CA16_2510")) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2510") %>% child_census_vectors(TRUE, keep_parent = TRUE) ## End(Not run)
A dataset with code table summaries for census data
derived from StatCan definitions
https://www12.statcan.gc.ca/census-recensement/2021/geo/ref/domain-domaine/index2021-eng.cfm?lang=e&id=CSDtype, https://www12.statcan.gc.ca/census-recensement/2021/geo/ref/domain-domaine/index2021-eng.cfm?lang=e&id=CDtype
A dataset City of Vancouver skytrain station locations
City of Vancouver Open Data
https://opendata.vancouver.ca/explore/dataset/rapid-transit-stations/information/
Get attribution for datasets
dataset_attribution(datasets)
dataset_attribution(datasets)
datasets |
Vector of dataset identifiers |
Returns a string to be used as attribution for the given datasets.
# Attribution string for the 2006 and 2016 census datasets dataset_attribution(c('CA06','CA16'))
# Attribution string for the 2006 and 2016 census datasets dataset_attribution(c('CA06','CA16'))
Finding the right Census variables or regions can be complicated.
explore_census_vectors(dataset)
and explore_census_regions(dataset)
will open a
new browser page or tab to an interactive Census variable and region exploration and selection
tool on the Censusmapper.ca website. Interactive
tools available for the CA16, CA11, CA06, and CA01 Census datasets and geographies.
explore_census_regions(dataset = "CA16")
explore_census_regions(dataset = "CA16")
dataset |
The dataset to query for available vectors, e.g. |
## Not run: explore_census_vectors(dataset = "CA16") explore_census_regions(dataset = "CA11") ## End(Not run)
## Not run: explore_census_vectors(dataset = "CA16") explore_census_regions(dataset = "CA11") ## End(Not run)
Finding the right Census variables or regions can be complicated.
explore_census_vectors(dataset)
and explore_census_regions(dataset)
will open a
new browser page or tab to an interactive Census variable and region exploration and selection
tool on the Censusmapper.ca website. Interactive
tools available for the CA16, CA11, CA06, and CA01 Census datasets and geographies.
explore_census_vectors(dataset = "CA16")
explore_census_vectors(dataset = "CA16")
dataset |
The dataset to query for available vectors, e.g. |
## Not run: explore_census_vectors(dataset = "CA16") explore_census_regions(dataset = "CA11") ## End(Not run)
## Not run: explore_census_vectors(dataset = "CA16") explore_census_regions(dataset = "CA11") ## End(Not run)
Query the available list of Census vectors based on their label and return
details including vector code. Default search behaviour expects an exact match, but
keyword or semantic searches can be used instead by setting query_type='keyword'
or
query_type = 'semantic'
instead. Keyword search is useful when looking to explore
Census vectors based on broad themes like "income" or "language". Keyword search separates
the query into unigrams and returns Census vectors with matching words, ranked by incidence
of matches. Semantic search is designed for more precise searches while allowing room for error
for spelling or phrasing, as well as for finding closely related vector matches. Semantic search
separates the query into n-grams and relies on string distance measurement using a generalized
Levenshtein distance approach.
Some census vectors return population counts segmented by Female
and Male
populations, in
addition to a total aggregate. By default, query matches will return matches for the Total
aggregation, but can optionally return only the Female
or Male
aggregations by adding
type = 'female'
or type = 'male'
as a parameter.
find_census_vectors(query, dataset, type = "all", query_type = "exact", ...)
find_census_vectors(query, dataset, type = "all", query_type = "exact", ...)
query |
The term or phrase to search for e.g. |
dataset |
The dataset to query for available vectors, e.g. |
type |
One of |
query_type |
One of |
... |
Other arguments passed to internal functions. |
find_census_vectors('Oji-cree', dataset = 'CA16', type = 'total', query_type = 'exact') find_census_vectors('commuting duration', dataset = 'CA11', type = 'female', query_type = 'keyword') find_census_vectors('after tax income', dataset = 'CA16', type = 'total', query_type = 'semantic') ## Not run: # This incorrect spelling will return a warning that no match was found, # but will suggest trying semantic or keyword search. find_census_vectors('Ojibwey', dataset = 'CA16', type = 'total') # This will find near matches as well find_census_vectors('Ojibwey', dataset = 'CA16', type = 'total', query_type = "semantic") find_census_vectors('commute duration', dataset = 'CA16', type = 'female', query_type = 'keyword') find_census_vectors('commute duration', dataset = 'CA11', type = 'all', query_type = 'keyword') find_census_vectors('ukrainian origin', dataset = 'CA16', type = 'total', query_type = 'keyword') ## End(Not run)
find_census_vectors('Oji-cree', dataset = 'CA16', type = 'total', query_type = 'exact') find_census_vectors('commuting duration', dataset = 'CA11', type = 'female', query_type = 'keyword') find_census_vectors('after tax income', dataset = 'CA16', type = 'total', query_type = 'semantic') ## Not run: # This incorrect spelling will return a warning that no match was found, # but will suggest trying semantic or keyword search. find_census_vectors('Ojibwey', dataset = 'CA16', type = 'total') # This will find near matches as well find_census_vectors('Ojibwey', dataset = 'CA16', type = 'total', query_type = "semantic") find_census_vectors('commute duration', dataset = 'CA16', type = 'female', query_type = 'keyword') find_census_vectors('commute duration', dataset = 'CA11', type = 'all', query_type = 'keyword') find_census_vectors('ukrainian origin', dataset = 'CA16', type = 'total', query_type = 'keyword') ## End(Not run)
This function allows convenient access to Canadian census data and boundary files through the CensusMapper API. An API key is required to retrieve data.
get_census( dataset, regions, level = NA, vectors = c(), geo_format = NA, resolution = "simplified", labels = "detailed", use_cache = TRUE, quiet = FALSE, api_key = Sys.getenv("CM_API_KEY") )
get_census( dataset, regions, level = NA, vectors = c(), geo_format = NA, resolution = "simplified", labels = "detailed", use_cache = TRUE, quiet = FALSE, api_key = Sys.getenv("CM_API_KEY") )
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 |
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 |
resolution |
Resolution of the geographic data. By default simplified geometries will be download. For lower level geometries like DB or DA this will be very close to the high resolution data.
Simplification generally increases as the geographic aggregation level increases.
If high resolution geometries are required
then this option can be set to 'high'. By default this setting is set to |
labels |
Set to "detailed" by default, but truncated Census variable names can be selected by setting labels = "short". Use |
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 |
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()
Census data and boundary geographies are reproduced and distributed on an "as is" basis with the permission of Statistics Canada (Statistics Canada 1996; 2001; 2006; 2011; 2016).
# 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) ## End(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) ## End(Not run)
This function returns a list of regions that intersect a given geometry input. This list of regions can be used
directly to query census when one is interested in census data for a particular geographic region that does
not coincide with defined census geometries. The returned value can be used as the regions
parameter in get_census to get corresponding census geographies and variables that cover the give geometry.
Input spatial objects can be any sf
or sfc
class objects such as POINT
, MULTIPOINT
or POLYGON
.
This function comes with CensusMapper API limits
get_intersecting_geometries( dataset, level, geometry, simplified = FALSE, use_cache = TRUE, quiet = FALSE, api_key = Sys.getenv("CM_API_KEY") )
get_intersecting_geometries( dataset, level, geometry, simplified = FALSE, use_cache = TRUE, quiet = FALSE, api_key = Sys.getenv("CM_API_KEY") )
dataset |
A CensusMapper dataset identifier. |
level |
The census aggregation level to retrieve. One of |
geometry |
A valid |
simplified |
If |
use_cache |
If set to |
quiet |
When TRUE, suppress messages and warnings. |
api_key |
An API key for the CensusMapper API. Defaults to |
Census data and boundary geographies are reproduced and distributed on an "as is" basis with the permission of Statistics Canada (Statistics Canada 1996; 2001; 2006; 2011; 2016).
## Not run: # Example using a POINT-class object from a pair of lat/lon coordinates point_geo <- sf::st_sfc(sf::st_point(c(-123.25149, 49.27026)), crs=4326) regions <- get_intersecting_geometries(dataset = 'CA16', level = 'CT', geometry = point_geo) census_data <- get_census(dataset='CA16', regions=regions, vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"), level='CT') # Example using a POLYGON-class object from a bounding box with four coordinates poly_geo <- sf::st_as_sfc(sf::st_bbox(c(ymin = 49.25, ymax = 49.30, xmin = -123.25, xmax = -123.30)), crs = 4326) regions <- get_intersecting_geometries(dataset = 'CA16', level = 'CT', geometry = poly_geo) census_data <- get_census(dataset='CA16', regions=regions, vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"), level='CT') ## End(Not run)
## Not run: # Example using a POINT-class object from a pair of lat/lon coordinates point_geo <- sf::st_sfc(sf::st_point(c(-123.25149, 49.27026)), crs=4326) regions <- get_intersecting_geometries(dataset = 'CA16', level = 'CT', geometry = point_geo) census_data <- get_census(dataset='CA16', regions=regions, vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"), level='CT') # Example using a POLYGON-class object from a bounding box with four coordinates poly_geo <- sf::st_as_sfc(sf::st_bbox(c(ymin = 49.25, ymax = 49.30, xmin = -123.25, xmax = -123.30)), crs = 4326) regions <- get_intersecting_geometries(dataset = 'CA16', level = 'CT', geometry = poly_geo) census_data <- get_census(dataset='CA16', regions=regions, vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"), level='CT') ## End(Not run)
Reads the geosuite data for the given level and census year. Data gets cached after first download if the cancensus cache path has been set. For older years 'get_statcan_geographic_attributes()' can fill in most of the information
get_statcan_geo_suite(level, census_year = "2021", refresh = FALSE)
get_statcan_geo_suite(level, census_year = "2021", refresh = FALSE)
level |
geographic level to return the data for, valid choices are "DB", "DA", "ADA", "CT", "CSD", "CMA", "CD", "PR", "FED", "DPL", "ER", "PN", "POPCTR" |
census_year |
census year to get the data for, right now only 2021 is supported |
refresh |
(logical) refresh the cache if true |
tibble with the geosuite data
# list add the cached census data ## Not run: get_statcan_geo_suite("DA","2021") ## End(Not run)
# list add the cached census data ## Not run: get_statcan_geo_suite("DA","2021") ## End(Not run)
Reads the Geographies Attributes File for the given census year. The table contains the information on how all the geographic levels are related for each area, and population, dwelling and household counts. Data gets cached after first download if the cancensus cache path has been set. A reference guide is available at https://www150.statcan.gc.ca/n1/en/catalogue/92-151-G2021001
get_statcan_geographic_attributes(census_year = "2021", refresh = FALSE)
get_statcan_geographic_attributes(census_year = "2021", refresh = FALSE)
census_year |
census year to get the data for, right now only 2006, 2011, 2016, 2021 are supported |
refresh |
(logical) refresh the cache if true |
tibble with the relationship data
# list add the cached census data ## Not run: get_statcan_geographic_attributes("2021") ## End(Not run)
# list add the cached census data ## Not run: get_statcan_geographic_attributes("2021") ## End(Not run)
Reads the original unprocessed geographic boundary files from Statistics Canada
get_statcan_geographies( census_year, level, type = "cartographic", cache_path = NULL, timeout = 1000, refresh = FALSE, quiet = FALSE )
get_statcan_geographies( census_year, level, type = "cartographic", cache_path = NULL, timeout = 1000, refresh = FALSE, quiet = FALSE )
census_year |
census year to get the data for, right now only 2021 is supported |
level |
geographic level to return the data for, valid choices are "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA" |
type |
type of geographic data, valid choices area "cartographic" or "digital" |
cache_path |
optional path to cache the data. If the cancensus cache path is set the geographic data gets cached in the "geographies" subdirectory of the cancensus cache path. |
timeout |
optional timeout parameter, adjust as needed if the data download times out when using slow connections |
refresh |
(logical) refresh the cache if true |
quiet |
(logical) suppress messages if 'TRUE' |
a spatial dataframe with the geographic data
# get the digital geographic boundaries for provinces and territories ## Not run: get_statcan_geographies(census_year="2021",level="PR",type="digital") ## End(Not run)
# get the digital geographic boundaries for provinces and territories ## Not run: get_statcan_geographies(census_year="2021",level="PR",type="digital") ## End(Not run)
Get official census data from Statistics Canada for a given set of DGUIDs. Only available for the 2021 census. The downloaded data gets enriched by geographic and characteristic names based on metadata obtained via 'get_statcan_wds_metadata()'. Data is cached for the duration of the R session.
get_statcan_wds_data( DGUIDs, members = NULL, version = NULL, gender = "All", language = "en", refresh = FALSE )
get_statcan_wds_data( DGUIDs, members = NULL, version = NULL, gender = "All", language = "en", refresh = FALSE )
DGUIDs |
census year to get the data for, right now only 2021 is supported. Valid DGUIDs for a given geographic level can be queried via 'get_statcan_wds_metadata()'. |
members |
list of Member IOs to download data for. By default all characteristics are downloaded. Valid Member IDs and their descriptions can be queried via the 'get_statcan_wds_metadata()' call. |
version |
optionally specify a version of the data to download. For example, for FED level data, version 1.3 will access the 2013 represenation order, whereas version 2.0 will access the 2023 representation order. By default the latest available version is accessed. |
gender |
optionally query data for only one gender. By default this queries data for all genders, possible values are "Total", "Male", "Female" to only query total data, or for males only or for females only. |
language |
specify language for geography and characteristic names that get added, valid choices are "en" and "fr" |
refresh |
default is 'FALSE' will refresh the temporary cache if 'TRUE' |
tibble with the enriched census data
# get data for federal electoral district 2013A000459021 ## Not run: get_statcan_wds_data(DGUIDs="2013A000459021",level="FED") ## End(Not run)
# get data for federal electoral district 2013A000459021 ## Not run: get_statcan_wds_data(DGUIDs="2013A000459021",level="FED") ## End(Not run)
Get official metadata information from Statistics Canada for a given geographic level. Only available for the 2021 census. Data is cached for the duration of the R session.
get_statcan_wds_metadata(census_year, level, version = NULL, refresh = FALSE)
get_statcan_wds_metadata(census_year, level, version = NULL, refresh = FALSE)
census_year |
census year to get the data for, right now only 2021 is supported |
level |
geographic level to return the data for, valid choices are "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA" |
version |
optionally specify a version of the data to download. For example, for FED level data, version 1.3 will access the 2013 represenation order, whereas version 2.0 will access the 2023 representation order. By default the latest available version is accessed. |
refresh |
default is 'FALSE' will refresh the temporary cache if 'TRUE' |
tibble with the metadata
# get metadata for federal electoral districts ## Not run: get_statcan_wds_metadata(census_year="2021",level="FED") ## End(Not run)
# get metadata for federal electoral districts ## Not run: get_statcan_wds_metadata(census_year="2021",level="FED") ## End(Not run)
Return Census variable names and labels as a tidy data frame
label_vectors(x)
label_vectors(x)
x |
A data frame, |
A data frame with a column variable
containing the truncated
variable name, and a column label
describing it.
## Not run: # Query census data with truncated labels: label_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(label_data) ## End(Not run)
## Not run: # Query census data with truncated labels: label_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(label_data) ## End(Not run)
Lists all cached data and metadata if available
list_cancensus_cache()
list_cancensus_cache()
tibble with metadata on cached data
# list add the cached census data list_cancensus_cache()
# list add the cached census data list_cancensus_cache()
Query the CensusMapper API for available datasets.
list_census_datasets(use_cache = TRUE, quiet = FALSE)
list_census_datasets(use_cache = TRUE, quiet = FALSE)
use_cache |
If set to TRUE (the default), data will be read from a temporary local cache for the duration of the R session, if available. If set to FALSE, query the API for the data, and refresh the temporary cache with the result. |
quiet |
When TRUE, suppress messages and warnings. |
Returns a data frame with a column dataset
containing the code for the
dataset, a column description
describing it, a geo_dataset
column
identifying the geography dataset the data is based on, a attribution
column
with an attribution string, a reference
column with a reference identifier, and
a reference_url
column with a link to reference materials.
# List available datasets in CensusMapper list_census_datasets()
# List available datasets in CensusMapper list_census_datasets()
Query the CensusMapper API for available regions in a given dataset.
list_census_regions(dataset, use_cache = TRUE, quiet = FALSE)
list_census_regions(dataset, use_cache = TRUE, quiet = FALSE)
dataset |
The dataset to query for available regions, e.g.
|
use_cache |
If set to TRUE (the default), data will be read from a local cache that is maintained for the duration of the R session, if available. If set to FALSE, query the API for the data, and refresh the local cache with the result. |
quiet |
When TRUE, suppress messages and warnings. |
Returns a data frame with the following columns:
region
The region identifier.
name
The name of that region.
level
The census aggregation level of that region.
pop
The population of that region.
municipal_status
Additional identifiers to distinguish the municipal status of census subdivisions.
CMA_UID
The identifier for the Census Metropolitan Area the region is in (if any).
CD_UID
The identifier for the Census District the region is in (if any).
PR_UID
The identifier for the Province the region is in (if unique).
## Not run: list_census_regions('CA16') ## End(Not run)
## Not run: list_census_regions('CA16') ## End(Not run)
Query the CensusMapper API for available vectors for a given dataset.
list_census_vectors(dataset, use_cache = TRUE, quiet = TRUE)
list_census_vectors(dataset, use_cache = TRUE, quiet = TRUE)
dataset |
The dataset to query for available vectors, e.g.
|
use_cache |
If set to TRUE (the default), data will be read from a local cache that is maintained for the duration of the R session, if available. If set to FALSE, query the API for the data, and refresh the local cache with the result. |
quiet |
When FALSE, shows messages and warnings. Set to TRUE by default. |
Returns a data frame detailing the available Census vectors (i.e. variables) for a given Census
dataset. This data frame has columns vector
containing the short code for the
variable, type
describing whether it's a female, male, or total aggregate, label
indicating the name of the variable, units
indicating whether the value represents a
numeric integer, percentage, dollar figure, or ratio, parent_vector
to show hierarchical
relationship, aggregation
indicating whether the value is additive or a transformation,
and a column details
with a detailed description of the variable generated by traversing
all labels within its hierarchical structure.
## Not run: # List all vectors for a given Census dataset in CensusMapper list_census_vectors('CA16') ## End(Not run)
## Not run: # List all vectors for a given Census dataset in CensusMapper list_census_vectors('CA16') ## End(Not run)
Checks the local cached database for recalled data and lists all recalled cached entries
list_recalled_cached_data( cached_data = list_cancensus_cache(), warn_only_once = FALSE )
list_recalled_cached_data( cached_data = list_cancensus_cache(), warn_only_once = FALSE )
cached_data |
List of locally cached data to check for recall, default is 'list_cancensus_cache()' in which case it will get checked against all locally cached data |
warn_only_once |
Will only warn on first run during each session, 'FALSE' by default |
tibble with rows describing locally cached recalled data
## Not run: list_recalled_cached_data() ## End(Not run)
## Not run: list_recalled_cached_data() ## End(Not run)
list_census_vectors
, search_census_vectors
, find_census_vectors
, or a direct string reference to the vector code.List all parent variables from vector hierarchies given either a list of Census
variables returned by
list_census_vectors
, search_census_vectors
, find_census_vectors
, or a direct string reference to the vector code.
parent_census_vectors(vector_list)
parent_census_vectors(vector_list)
vector_list |
The list of vectors to be used, either a character vector or a filtered tibble
as returned from |
# Query parent vectors directly using vector identifier parent_census_vectors("v_CA16_2519") ## Not run: # Example using multiple vectors coerced into a list parent_census_vectors(c("v_CA16_2519","v_CA16_2520","v_CA16_2521")) # or, equivalently selected_vectors <- c("v_CA16_2519","v_CA16_2520","v_CA16_2521") parent_census_vectors(selected_vectors) # Example using dplyr and piped arguments library(dplyr, warn.conflicts = FALSE) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2519") %>% parent_census_vectors() ## End(Not run)
# Query parent vectors directly using vector identifier parent_census_vectors("v_CA16_2519") ## Not run: # Example using multiple vectors coerced into a list parent_census_vectors(c("v_CA16_2519","v_CA16_2520","v_CA16_2521")) # or, equivalently selected_vectors <- c("v_CA16_2519","v_CA16_2520","v_CA16_2521") parent_census_vectors(selected_vectors) # Example using dplyr and piped arguments library(dplyr, warn.conflicts = FALSE) list_census_vectors("CA16") %>% filter(vector == "v_CA16_2519") %>% parent_census_vectors() ## End(Not run)
Remove cached data for paths given
remove_from_cancensus_cache(paths)
remove_from_cancensus_cache(paths)
paths |
list of paths to remove, as returned by the path column in 'list_cancensus_cache' |
freed-up disk space
## Not run: # remove the first cached dataset cache_data <- list_cancensus_cache() remove_from_cancensus_cache(cache_data$path[1]) ## End(Not run)
## Not run: # remove the first cached dataset cache_data <- list_cancensus_cache() remove_from_cancensus_cache(cache_data$path[1]) ## End(Not run)
Checks the local cached database for recalled data and removes cached data that has been recalled
remove_recalled_cached_data()
remove_recalled_cached_data()
Storage size of removed locally cached data that got freed up in number of bytes.
## Not run: remove_recalled_cached_data() ## End(Not run)
## Not run: remove_recalled_cached_data() ## End(Not run)
Runs a query against the CensusMapper API to retrieve region data with
names matching specific queries. Users can optionally specify the target geography level
(e.g. level = 'CMA'
, level = 'CSD'
, etc.). Alternatively, calling
explore_census_vectors()
will launch the interactive region selection tool on
the CensusMapper site in a new web page or tab.
search_census_regions(searchterm, dataset, level = NA, ...)
search_census_regions(searchterm, dataset, level = NA, ...)
searchterm |
The term to search for e.g. |
dataset |
The dataset to query for available regions, e.g.
|
level |
One of |
... |
Further arguments passed on to |
A census region list of the same format as 'list_census_regions()' containing the matches.
## Not run: # This will return a warning that no match was found, but will suggest similar named regions. search_census_regions('Victorea', 'CA16') # This will limit region results to only include CMA level regions search_census_regions('Victoria', 'CA16', level = "CMA") ## End(Not run)
## Not run: # This will return a warning that no match was found, but will suggest similar named regions. search_census_regions('Victorea', 'CA16') # This will limit region results to only include CMA level regions search_census_regions('Victoria', 'CA16', level = "CMA") ## End(Not run)
Query the CensusMapper API for vectors with descriptions matching a search term or phrase (deprecated)
search_census_vectors(searchterm, dataset, type = NA, ...)
search_census_vectors(searchterm, dataset, type = NA, ...)
searchterm |
The term or phrase to search for e.g. |
dataset |
The dataset to query for available vectors, e.g.
|
type |
One of |
... |
Further arguments passed on to |
search_census_vectors('Ojibway', 'CA16') ## Not run: # This will return a warning that no match was found, but will suggest similar terms. search_census_vectors('Ojibwe', 'CA16', 'Total') ## End(Not run)
search_census_vectors('Ojibway', 'CA16') ## Not run: # This will return a warning that no match was found, but will suggest similar terms. search_census_vectors('Ojibwe', 'CA16', 'Total') ## End(Not run)
Cancensus requires a free Censusmapper API key to retrieve data. This function helps set the key for either the duration of the session (default) or permanently for use across sessions.
set_cancensus_api_key(key, overwrite = FALSE, install = FALSE)
set_cancensus_api_key(key, overwrite = FALSE, install = FALSE)
key |
a Censusmapper API key. For more information on keys see the API key section |
overwrite |
Option to overwrite any existing Censusmapper keys already stored locally. |
install |
Option to install permanently for use across sessions. |
## Not run: set_cancensus_api_key("YOUR_CM_API_KEY") # This will set the key permanently until overwritten again set_cancensus_api_key("YOUR_CM_API_KEY", install = TRUE) ## End(Not run)
## Not run: set_cancensus_api_key("YOUR_CM_API_KEY") # This will set the key permanently until overwritten again set_cancensus_api_key("YOUR_CM_API_KEY", install = TRUE) ## End(Not run)
Cancensus provides session caching for retrieved data to increase speeds and reduce API usage. This function will create a persistent cache across sessions.
set_cancensus_cache_path(cache_path, overwrite = FALSE, install = FALSE)
set_cancensus_cache_path(cache_path, overwrite = FALSE, install = FALSE)
cache_path |
a local directory to use for saving cached data |
overwrite |
Option to overwrite any existing cache path already stored locally. |
install |
Option to install permanently for use across sessions. |
## Not run: set_cancensus_cache_path("~/cancensus_cache") # This will set the cache path permanently until overwritten again set_cancensus_cache_path("~/cancensus_cache", install = TRUE) ## End(Not run)
## Not run: set_cancensus_cache_path("~/cancensus_cache") # This will set the cache path permanently until overwritten again set_cancensus_cache_path("~/cancensus_cache", install = TRUE) ## End(Not run)
View saved API key'
show_cancensus_api_key()
show_cancensus_api_key()
show_cancensus_api_key()
show_cancensus_api_key()
View saved cache path'
show_cancensus_cache_path()
show_cancensus_cache_path()
show_cancensus_cache_path()
show_cancensus_cache_path()