Skip to content

Commit

Permalink
Merge pull request #205 from mountainMath/v0.5.7
Browse files Browse the repository at this point in the history
V0.5.7
  • Loading branch information
mountainMath authored Feb 10, 2024
2 parents 40f4ba9 + 6ef477f commit 2833171
Show file tree
Hide file tree
Showing 74 changed files with 384 additions and 244 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ data_cache
R/local_server_setup.R
inst/doc
inst/cache
cancensus.Rproj
doc
Meta
CRAN-RELEASE
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: cancensus
Type: Package
Title: Access, Retrieve, and Work with Canadian Census Data and Geography
Version: 0.5.6
Version: 0.5.7
Authors@R: c(
person("Jens", "von Bergmann", email = "[email protected]", role = c("aut"), comment = "API creator and maintainer"),
person("Dmitry", "Shkolnik", email = "[email protected]", role = c("aut", "cre"), comment = "Package maintainer, responsible for correspondence"),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# cancensus 0.5.7

- fix issue with path names not quoted properly when downoading and unpacking geosuite data
- support versions in WDS data, in paricular for federal electoral districts

# cancensus 0.5.6

- fix issue when using named vectors to query data for non-existent geographies, return NULL in this case instead of throwing error
Expand Down
2 changes: 1 addition & 1 deletion R/cancensus.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @param level The census aggregation level to retrieve, defaults to \code{"Regions"}. One of \code{"Regions"}, \code{"PR"}, \code{"CMA"}, \code{"CD"}, \code{"CSD"}, \code{"CT"}, \code{"DA"}, \code{"EA"} (for 1996), or \code{"DB"} (for 2001-2016).
#' @param 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.
#' @param geo_format By default is set to \code{NA} and appends no geographic information. To include geographic information with census data, specify one of either \code{"sf"} to return an \code{\link[sf]{sf}} object (requires the \code{sf} package) or \code{"sp"} to return a \code{\link[sp]{SpatialPolygonsDataFrame-class}} object (requires the \code{rgdal} package). If user requests geo-spatial data and neither package is available, a context menu will prompt to install the \code{sf} package.
#' @param resolution Resolution of the geographic data. {cancensus} will download simplified geometries by default. For lower level geometries like DB or DA this will be very close to the high resolution data.
#' @param 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 \code{'simplified'}.
Expand Down
2 changes: 1 addition & 1 deletion R/geo_suite.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ get_statcan_geo_suite <- function(level,census_year="2021",refresh=FALSE){
options(timeout=old_timeout)
if (!dir.exists(path)) dir.create(path)
if (Sys.info()[['sysname']]=="Darwin") {
system(paste0("ditto -V -x -k --sequesterRsrc --rsrc ",tmp," ",path))
system(paste0("ditto -V -x -k --sequesterRsrc --rsrc '",tmp,"' '",path,"'"))
} else {
utils::unzip(tmp,exdir = path)
}
Expand Down
17 changes: 13 additions & 4 deletions R/wds.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#' @param census_year census year to get the data for, right now only 2021 is supported
#' @param level geographic level to return the data for, valid choices are
#' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA"
#' @param 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.
#' @param refresh default is `FALSE` will refresh the temporary cache if `TRUE`
#' @return tibble with the metadata
#'
Expand All @@ -16,7 +19,7 @@
#' get_statcan_wds_metadata(census_year="2021",level="FED")
#' }
#' @export
get_statcan_wds_metadata <- function(census_year,level,refresh=FALSE){
get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALSE){
valid_census_years <- c("2021")
valid_levels <- c("PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR","FSA")
if (!(census_year %in% valid_census_years)) {
Expand All @@ -25,7 +28,9 @@ get_statcan_wds_metadata <- function(census_year,level,refresh=FALSE){
if (!(level %in% valid_levels)) {
stop(paste0("Level must be one of ",paste0(valid_levels,collapse = ", "),"."))
}
meta_url <- paste0("https://api.statcan.gc.ca/census-recensement/profile/sdmx/rest/dataflow/STC_CP/DF_",level,"?references=all")
meta_url <- paste0("https://api.statcan.gc.ca/census-recensement/profile/sdmx/rest/dataflow/STC_CP/DF_",level,
ifelse(is.null(version),"",paste0("/",version)),
"?references=all")
metadata_tempfile <- file.path(tempdir(),paste0("census_wds_metadata_",digest::digest(meta_url),".sdmx"))
if (refresh || !file.exists(metadata_tempfile)) {
utils::download.file(meta_url,metadata_tempfile)
Expand Down Expand Up @@ -67,6 +72,9 @@ get_statcan_wds_metadata <- function(census_year,level,refresh=FALSE){
#' level can be queried via `get_statcan_wds_metadata()`.
#' @param 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.
#' @param 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.
#' @param 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.
#' @param language specify language for geography and characteristic names that get added, valid choices are "en" and "fr"
Expand All @@ -81,6 +89,7 @@ get_statcan_wds_metadata <- function(census_year,level,refresh=FALSE){
#' @export
get_statcan_wds_data <- function(DGUIDs,
members = NULL,
version= NULL,
gender="All",
language="en",
refresh=FALSE) {
Expand All @@ -102,7 +111,7 @@ get_statcan_wds_data <- function(DGUIDs,
gender <- c("All"="","Total"="1","Male"="2","Female"="3")[[gender]]
dguid_string <- paste0(DGUIDs,collapse="+")
member_string <- paste0(members,collapse = "+")
add=paste0("DF_",level,"/A5.",dguid_string,".",gender,".",member_string,".1")
add=paste0("DF_",level,ifelse(is.null(version),"",paste0(",",version)),"/A5.",dguid_string,".",gender,".",member_string,".1")
wds_data_tempfile <- file.path(tempdir(),paste0("wds_data_",digest::digest(add),".csv"))
if (!file.exists(wds_data_tempfile)) {
response <- httr::GET(paste0(url,",",add),
Expand All @@ -114,7 +123,7 @@ get_statcan_wds_data <- function(DGUIDs,
stop(paste0("Invalid request.\n",httr::content(response)))
}
census_year <- "2021"
meta_data <- get_statcan_wds_metadata(census_year,level,refresh = refresh)
meta_data <- get_statcan_wds_metadata(census_year,level=level,version = version,refresh = refresh)

levels <- meta_data %>%
dplyr::filter(.data$`Codelist ID`=="CL_GEO_LEVEL")
Expand Down
27 changes: 11 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cancensus

[![R build status](https://github.com/mountainMath/cancensus/workflows/R-CMD-check/badge.svg)](https://github.com/mountainMath/cancensus/actions)
[![R-CMD-check](https://github.com/mountainMath/cancensus/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mountainMath/cancensus/actions/workflows/R-CMD-check.yaml)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/cancensus)](https://cran.r-project.org/package=cancensus)
[![CRAN_Downloads_Badge](https://cranlogs.r-pkg.org/badges/cancensus)](https://cranlogs.r-pkg.org/badges/cancensus)

Expand Down Expand Up @@ -51,7 +51,7 @@ Starting with version 0.5.2 **cancensus** will automatically check if for data t

### Currently available datasets

**cancensus** can access Statistics Canada Census data for Census years 1996, 2001, 2006, 2011, 2016, and 2021. You can run `list_census_datasets` to check what datasets are currently available for access through the CensusMapper API. Additional data for the 2021 Census will be included in CensusMapper within a day or two after public release by Statistics Canada. Statistics Canada maintains a release schedule for the Census 2021 Program which can be viewed on their [website](https://www12.statcan.gc.ca/census-recensement/2021/ref/prodserv/release-diffusion-eng.cfm).
**cancensus** can access Statistics Canada Census data for Census years 1996, 2001, 2006, 2011, 2016, and 2021. You can run `list_census_datasets` to check what datasets are currently available for access through the CensusMapper API.

Thanks to contributions by the Canada Mortgage and Housing Corporation (CMHC), **cancensus** now includes additional Census-linked datasets as open-data releases. These include annual taxfiler data at the census tract level for tax years 2000 through 2018, which includes data on incomes and demographics, as well as specialized crosstabs for Structural type of dwelling by Document type, which details occupancy status for residences. These crosstabs are available for the 2001, 2006, 2011, 2016, and 2021 Census years at all levels starting with census tract.

Expand Down Expand Up @@ -105,14 +105,17 @@ We'd love to feature examples of work or projects that use cancensus.


### Related packages
This package is designed to play well with other Canadian data packages, in particular
The cancensus package is designed for working with Canadian Census data. In addition to Census data, Statistics Canada provides access to a vast [socio-economic data repository](https://www150.statcan.gc.ca/n1/en/type/data) with thousands of data tables available for public access.

The [cansim package](https://mountainmath.github.io/cansim/index.html) is designed to retrieve and work with public Statistics Canada data tables. The cansim prepares retrieved data tables as analysis-ready tidy dataframes and provides a number of convenience tools and functions to make it easier to work with Statistics Canada data. It is available on CRAN and on [Github](https://github.com/mountainMath/cancensus). Data retrieved via the cansim package can be linked to census data via the `GeoUID` field.

* [**tongfen**](https://mountainmath.github.io/tongfen/index.html), which solves the problem of making data across several census years comparable,
* [**cansim**](https://mountainmath.github.io/cansim/index.html), which accesses regular StatCan tables and can be linked to census geographies via the `GeoUID`,and
* [**cmhc**](https://mountainmath.github.io/cmhc/index.html), which accesses CMHC data and can be linked to census geographies via the `GeoUID`.
The [tongfen package](https://mountainmath.github.io/tongfen/index.html) automates the task of obtaining census variables from different census years on a common geography. It is available on [Github](https://github.com/mountainMath/tongfen).

The [**cmhc**](https://mountainmath.github.io/cmhc/index.html) package, which accesses CMHC data on the housing in Canada and can be linked to census geographies via the `GeoUID`.

Taken together these packages offer a seamless view into important Canadian data.

### International census data
There are several other jurisdiction where census data is available via R packages, including

* US: [tidycensus](https://walker-data.com/tidycensus/) and [tigris](https://github.com/walkerke/tigris)
Expand All @@ -139,7 +142,7 @@ There are several other jurisdiction where census data is available via R packag
If you wish to cite cancensus:

von Bergmann, J., Aaron Jacobs, Dmitry Shkolnik (2022). cancensus: R package to
access, retrieve, and work with Canadian Census data and geography. v0.5.6.
access, retrieve, and work with Canadian Census data and geography. v0.5.7.


A BibTeX entry for LaTeX users is
Expand All @@ -148,19 +151,11 @@ A BibTeX entry for LaTeX users is
author = {Jens {von Bergmann} and Dmitry Shkolnik and Aaron Jacobs},
title = {cancensus: R package to access, retrieve, and work with Canadian Census data and geography},
year = {2022},
note = {R package version 0.5.6},
note = {R package version 0.5.7},
url = {https://mountainmath.github.io/cancensus/}
}
```
### Related packages

The cancensus package is designed for working with Canadian Census data. In addition to Census data, Statistics Canada provides access to a vast [socio-economic data repository](https://www150.statcan.gc.ca/n1/en/type/data) with thousands of data tables available for public access.

The [cansim package](https://mountainmath.github.io/cansim/index.html) is designed to retrieve and work with public Statistics Canada data tables. The cansim prepares retrieved data tables as analysis-ready tidy dataframes and provides a number of convenience tools and functions to make it easier to work with Statistics Canada data. It is available on CRAN and on [Github](https://github.com/mountainMath/cancensus).

Data downloaded through the cansim package that comes with standard geographic attributes will typically share a common geographic ID that can be matched to Census data.

The [tongfen package](https://mountainmath.github.io/tongfen/index.html) automates the task of obtaining census variables from different census years on a common geography. It is available on [Github](https://github.com/mountainMath/tongfen).

### Statistics Canada Attribution

Expand Down
20 changes: 20 additions & 0 deletions cancensus.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
4 changes: 4 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# cancensus 0.5.7
- fix issue with path names not quoted properly when downoading and unpacking geosuite data
- support versions in WDS data, in paricular for federal electoral districts

# cancensus 0.5.6
- fix issue when using named vectors to query data for non-existent geographies, return NULL in this case instead of throwing error
- fix problem with population centre geographic data download
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions docs/articles/Making_maps_with_cancensus.html

Large diffs are not rendered by default.

Loading

0 comments on commit 2833171

Please sign in to comment.