Skip to content

Commit 656a3cd

Browse files
authored
Merge pull request #214 from mountainMath/0.5.9
0.5.10
2 parents 9f39194 + 4b97090 commit 656a3cd

90 files changed

Lines changed: 1153 additions & 1300 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.Rbuildignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lastMiKTeXException
2222
^data-raw/*
2323
^\.renviron
2424
^\.rprofile
25-
25+
.DS_Store
2626
^doc$
2727

2828
^CRAN-SUBMISSION$

.github/workflows/R-CMD-check.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ on:
1010

1111
name: R-CMD-check
1212

13+
permissions: read-all
14+
1315
jobs:
1416
R-CMD-check:
1517
runs-on: ${{ matrix.config.os }}
@@ -20,7 +22,7 @@ jobs:
2022
fail-fast: false
2123
matrix:
2224
config:
23-
- {os: macOS-latest, r: 'release'}
25+
- {os: macos-latest, r: 'release'}
2426
- {os: windows-latest, r: 'release'}
2527
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
2628
- {os: ubuntu-latest, r: 'release'}
@@ -33,7 +35,7 @@ jobs:
3335
R_KEEP_PKG_SOURCE: yes
3436

3537
steps:
36-
- uses: actions/checkout@v2
38+
- uses: actions/checkout@v4
3739

3840
- uses: r-lib/actions/setup-pandoc@v2
3941

@@ -51,3 +53,4 @@ jobs:
5153
- uses: r-lib/actions/check-r-package@v2
5254
with:
5355
upload-snapshots: true
56+
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'

DESCRIPTION

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: cancensus
22
Type: Package
33
Title: Access, Retrieve, and Work with Canadian Census Data and Geography
4-
Version: 0.5.7
4+
Version: 0.5.10
55
Authors@R: c(
66
person("Jens", "von Bergmann", email = "jens@mountainmath.ca", role = c("aut"), comment = "API creator and maintainer"),
77
person("Dmitry", "Shkolnik", email = "shkolnikd@gmail.com", role = c("aut", "cre"), comment = "Package maintainer, responsible for correspondence"),
@@ -12,34 +12,34 @@ Description: Integrated, convenient, and uniform access to Canadian
1212
for working with Census variables, variable hierarchies, and region selection. API
1313
keys are freely available with free registration at <https://censusmapper.ca/api>.
1414
Census data and boundary geometries are reproduced and distributed on an "as
15-
is" basis with the permission of Statistics Canada (Statistics Canada 2001; 2006;
15+
is" basis with the permission of Statistics Canada (Statistics Canada 1996; 2001; 2006;
1616
2011; 2016; 2021).
1717
License: MIT + file LICENSE
1818
Encoding: UTF-8
1919
LazyData: yes
2020
ByteCompile: yes
2121
NeedsCompilation: no
2222
Imports: digest (>= 0.1),
23-
dplyr (>= 0.7),
23+
dplyr (>= 1.0),
2424
httr (>= 1.0.0),
2525
jsonlite (>= 1.0),
26-
rlang
27-
RoxygenNote: 7.2.3
26+
sf,
27+
geojsonsf,
28+
rlang,
29+
readr
30+
RoxygenNote: 7.3.3
2831
Suggests: knitr,
2932
ggplot2,
3033
leaflet,
3134
mapdeck,
3235
rmarkdown,
33-
readr,
3436
scales,
3537
sp,
36-
sf,
37-
geojsonsf,
3838
tidyr,
3939
lwgeom,
4040
xml2
4141
VignetteBuilder: knitr
4242
URL: https://github.com/mountainMath/cancensus, https://mountainmath.github.io/cancensus/, https://censusmapper.ca/api
4343
BugReports: https://github.com/mountainMath/cancensus/issues
4444
Depends:
45-
R (>= 2.10)
45+
R (>= 4.1)

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# cancensus 0.5.10
2+
3+
- ensure that cancensus won't trigger CRAN check errors if StatCan servers are down
4+
- replace shorthand function with regular function definition to address CRAN note
5+
6+
# cancensus 0.5.8
7+
8+
- clarify installing API key and cache path in "getting started" vignette
9+
- fix caching problem with get_statcan_wds_data
10+
- support health region level census data for 2021 via WDS
11+
- support download of dissemination block geographies from StatCan
12+
113
# cancensus 0.5.7
214

315
- fix issue with path names not quoted properly when downoading and unpacking geosuite data

R/cancensus.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ VALID_LEVELS <- c("Regions","C","PR", "CMA", "CD", "CSD", "ADA","CT", "DA", 'EA'
340340
#' @examples
341341
#'
342342
#' # List available datasets in CensusMapper
343+
#' \dontrun{
343344
#' list_census_datasets()
345+
#' }
344346
list_census_datasets <- function(use_cache = TRUE, quiet = FALSE) {
345347
cache_file <- file.path(tempdir(),"cancensus_datasets.rda") #cache_path("datasets.rda")
346348
if (!use_cache || !file.exists(cache_file)) {
@@ -379,8 +381,10 @@ list_census_datasets <- function(use_cache = TRUE, quiet = FALSE) {
379381
#'
380382
#' @examples
381383
#'
384+
#' \dontrun{
382385
#' # Attribution string for the 2006 and 2016 census datasets
383386
#' dataset_attribution(c('CA06','CA16'))
387+
#' }
384388
dataset_attribution <- function(datasets){
385389
datasets <- lapply(datasets,translate_dataset) %>% unlist()
386390
attribution <-list_census_datasets(quiet=TRUE) %>%

R/geo_suite.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ get_statcan_geography_relationships <- function(census_year="2021", refresh=FALS
101101
file_path <- cache_path(paste0("geography_relationship_",census_year,".zip"))
102102
if (refresh || !file.exists(file_path)) {
103103
url <- paste0("https://www12.statcan.gc.ca/census-recensement/",census_year,"/geo/sip-pis/dguid-idugd/files-fichiers/",census_year,"_98260004.zip")
104-
utils::download.file(url, file_path)
104+
utils::download.file(url, file_path,mode="wb")
105105
}
106106
readr::read_csv(file_path,
107107
locale = readr::locale(encoding ="Windows-1252"),
@@ -146,7 +146,7 @@ get_statcan_geographic_attributes <- function(census_year="2021",refresh=FALSE){
146146
if (dir.exists(base_path_year)) unlink(base_path_year,recursive = TRUE)
147147
dir.create(base_path_year)
148148
tmp<-tempfile(fileext = ".zip")
149-
utils::download.file(urls[[census_year]],tmp)
149+
status <- utils::download.file(urls[[census_year]],tmp,mode="wb")
150150
utils::unzip(tmp,exdir = base_path_year)
151151
}
152152
if (census_year=="2021") file <- dir(base_path_year,pattern="\\.csv",full.names = TRUE)

R/geographies.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#'
66
#' @param census_year census year to get the data for, right now only 2021 is supported
77
#' @param level geographic level to return the data for, valid choices are
8-
#' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA"
8+
#' "PR","CD","CMACA","CSD","CT","ADA","DA","DB,"ER","FED","DPL","POPCNTR", "FSA", "HR"
99
#' @param type type of geographic data, valid choices area "cartographic" or "digital"
1010
#' @param cache_path optional path to cache the data. If the cancensus cache path is set the geographic data gets
1111
#' cached in the "geographies" subdirectory of the cancensus cache path.
@@ -24,7 +24,7 @@ get_statcan_geographies <- function(census_year,level,type="cartographic",
2424
cache_path = NULL,timeout=1000,
2525
refresh=FALSE,quiet=FALSE) {
2626
valid_census_years <- c("2021")
27-
valid_levels <- c("PR","CD","CMACA","CMA","CA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR","POPCTR","FSA")
27+
valid_levels <- c("PR","CD","CMACA","CMA","CA","CSD","CT","ADA","DA","DB","ER","FED","DPL","POPCNTR","POPCTR","FSA","HR")
2828
valid_types <- c("cartographic","digital")
2929
if (!(census_year %in% valid_census_years)) {
3030
stop(paste0("Census year must be one of ",paste0(valid_census_years,collapse = ", "),"."))
@@ -51,6 +51,13 @@ get_statcan_geographies <- function(census_year,level,type="cartographic",
5151
stop(paste0("Problem, don't know how to get geographic data for level ",level,"."))
5252
}
5353
url <- paste0("https://www12.statcan.gc.ca/census-recensement/",census_year,"/geo/sip-pis/boundary-limites/files-fichiers/l",tolower(level),filler,typeID,"21a_e.zip")
54+
if (level=="HR") {
55+
if (type=="cartographic") {
56+
url <- "https://www150.statcan.gc.ca/n1/en/pub/82-402-x/2024001/hrbf-flrs/carto/ArcGIS/HR_000b23a_e.zip?st=wQRRL7PQ"
57+
} else {
58+
url <- "https://www150.statcan.gc.ca/n1/en/pub/82-402-x/2024001/hrbf-flrs/digi/ArcGIS/HR_000a23a_e.zip"
59+
}
60+
}
5461
tmp <- tempfile()
5562
options(timeout = timeout)
5663
utils::download.file(url,tmp,mode="wb",quiet=quiet)

R/vector_discovery.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
#' @export
1313
#'
1414
#' @examples
15-
#' search_census_vectors('Ojibway', 'CA16')
1615
#'\dontrun{
16+
#' search_census_vectors('Ojibway', 'CA16')
17+
#'
1718
#' # This will return a warning that no match was found, but will suggest similar terms.
1819
#' search_census_vectors('Ojibwe', 'CA16', 'Total')
1920
#' }
@@ -82,13 +83,13 @@ search_census_vectors <- function(searchterm, dataset, type=NA, ...) {
8283
#' @export
8384
#'
8485
#' @examples
86+
#'\dontrun{
8587
#' find_census_vectors('Oji-cree', dataset = 'CA16', type = 'total', query_type = 'exact')
8688
#'
8789
#' find_census_vectors('commuting duration', dataset = 'CA11', type = 'female', query_type = 'keyword')
8890
#'
8991
#' find_census_vectors('after tax income', dataset = 'CA16', type = 'total', query_type = 'semantic')
9092
#'
91-
#'\dontrun{
9293
#' # This incorrect spelling will return a warning that no match was found,
9394
#' # but will suggest trying semantic or keyword search.
9495
#' find_census_vectors('Ojibwey', dataset = 'CA16', type = 'total')

R/wds.R

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#'
77
#' @param census_year census year to get the data for, right now only 2021 is supported
88
#' @param level geographic level to return the data for, valid choices are
9-
#' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA"
9+
#' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA", "HR"
1010
#' @param version optionally specify a version of the data to download. For example, for FED level data, version 1.3 will
1111
#' access the 2013 represenation order, whereas version 2.0 will access the 2023 representation order. By default the latest
1212
#' available version is accessed.
@@ -21,7 +21,7 @@
2121
#' @export
2222
get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALSE){
2323
valid_census_years <- c("2021")
24-
valid_levels <- c("PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR","FSA")
24+
valid_levels <- c("PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR","FSA","HR")
2525
if (!(census_year %in% valid_census_years)) {
2626
stop(paste0("Census year must be one of ",paste0(valid_census_years,collapse = ", "),"."))
2727
}
@@ -38,7 +38,7 @@ get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALS
3838
d <- xml2::read_xml(metadata_tempfile)
3939
code_lists <- xml2::xml_find_all(d,"//structure:Codelist")
4040

41-
meta_data <- lapply(code_lists, \(cl){
41+
meta_data <- lapply(code_lists, function(cl){
4242
codelist_id <- cl %>% xml2::xml_attr("id")
4343
agencyID <- cl %>% xml2::xml_attr("agencyID")
4444
codelist_en <- cl %>% xml2::xml_find_all("common:Name[@xml:lang='en']") %>% xml2::xml_text()
@@ -54,7 +54,7 @@ get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALS
5454
en=codes %>% xml2::xml_find_all("common:Name[@xml:lang='en']") %>% xml2::xml_text(),
5555
fr=codes %>% xml2::xml_find_all("common:Name[@xml:lang='fr']") %>% xml2::xml_text(),
5656
`Parent ID`=codes %>% xml2::xml_find_all("structure:Parent/Ref",flatten=FALSE) %>%
57-
lapply(\(d)ifelse(is.null(d),NA,xml2::xml_attr(d,"id"))) %>% unlist()
57+
lapply(function(d)ifelse(is.null(d),NA,xml2::xml_attr(d,"id"))) %>% unlist()
5858
)
5959
}) %>%
6060
dplyr::bind_rows()
@@ -118,9 +118,17 @@ get_statcan_wds_data <- function(DGUIDs,
118118
httr::accept("text/csv"),
119119
httr::add_headers("Accept-Encoding"="deflate, gzip, br"),
120120
httr::write_disk(wds_data_tempfile,overwrite = TRUE))
121-
}
122-
if (!response$status_code=="200") {
123-
stop(paste0("Invalid request.\n",httr::content(response)))
121+
if (!response$status_code=="200") {
122+
if (!is.null(response$error) && ("curl_error_peer_failed_verification" %in% class(response$error))) {
123+
stop(paste0(strwrap(gsub(".+\\): ","",as.character(response$error),80)),collapse = "\n"),"\n",
124+
"This means that the authenticity of the StatCan API server can't be verified.\n",
125+
"Statistics Canada has a history of failty SSL certificats on their API,\n",
126+
"if you are reasonably sure that your connection is not getting hijacked you\n",
127+
"can disable peer checking for the duration of the R session by typing\n\n",
128+
"httr::set_config(httr::config(ssl_verifypeer=0,ssl_verifystatus=0))","\n\n","into the console.")
129+
}
130+
stop(paste0("Invalid request.\n",httr::content(response)))
131+
}
124132
}
125133
census_year <- "2021"
126134
meta_data <- get_statcan_wds_metadata(census_year,level=level,version = version,refresh = refresh)

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ We'd love to feature examples of work or projects that use cancensus.
104104
* [Additional datasets: Annual T1FF taxfiler data](https://mountainmath.github.io/cancensus/articles/Taxfiler_Data.html)
105105

106106

107+
### Python port
108+
A Python port of **cancensus** is available via the [pycancensus package](https://github.com/dshkol/pycancensus/).
109+
107110
### Related packages
108111
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.
109112

@@ -142,7 +145,7 @@ There are several other jurisdiction where census data is available via R packag
142145
If you wish to cite cancensus:
143146

144147
von Bergmann, J., Aaron Jacobs, Dmitry Shkolnik (2025). cancensus: R package to
145-
access, retrieve, and work with Canadian Census data and geography. v0.5.9.
148+
access, retrieve, and work with Canadian Census data and geography. v0.5.10.
146149

147150

148151
A BibTeX entry for LaTeX users is
@@ -151,7 +154,8 @@ A BibTeX entry for LaTeX users is
151154
author = {Jens {von Bergmann} and Dmitry Shkolnik and Aaron Jacobs},
152155
title = {cancensus: R package to access, retrieve, and work with Canadian Census data and geography},
153156
year = {2025},
154-
note = {R package version 0.5.9},
157+
doi = {10.32614/CRAN.package.cancensus},
158+
note = {R package version 0.5.10},
155159
url = {https://mountainmath.github.io/cancensus/}
156160
}
157161
```

0 commit comments

Comments
 (0)