Skip to content

Commit 42bf367

Browse files
authored
Merge pull request #163 from mountainMath/v0.4.1
fix problem with api key and remove funky warning
2 parents 851ef3e + 88bf06c commit 42bf367

56 files changed

Lines changed: 557 additions & 554 deletions

Some content is hidden

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

DESCRIPTION

Lines changed: 1 addition & 1 deletion
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.4.0
4+
Version: 0.4.1
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"),

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# cancensus 0.4.1
2+
3+
## Minor changes
4+
- Fix minor problem where API key wasn't always picked up if not set correctly as environment variable.
5+
- Fix warning when `t` column not present in downloaded data.
6+
17
# cancensus 0.4.0
28

39
## Major changes

R/cancensus.R

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA, labels = "detailed",
4848
use_cache=TRUE, quiet=FALSE, api_key=Sys.getenv("CM_API_KEY")) {
4949
api_key <- robust_api_key(api_key)
50-
have_api_key <- !is.null(api_key)
50+
have_api_key <- valid_api_key(api_key)
5151
result <- NULL
5252

5353
if (is.na(level)) level="Regions"
@@ -95,8 +95,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
9595
digest::digest(param_string, algo = "md5"), ".rda")
9696
if (!use_cache || !file.exists(data_file)) {
9797
if (!have_api_key) {
98-
stop(paste("No API key set. Use Sys.setenv(CM_API_KEY = '<your API key>') or",
99-
"options(cancensus.api_key = '<your API key>') to set one."))
98+
stop(paste("No API key set. Use set_api_key('<your API ket>`) to set one, or set_api_key('<your API ket>`, install = TRUE) to save is permanently in our .Renviron."))
10099
}
101100
url <- paste0(base_url, "data.csv")
102101
response <- if (!quiet) {
@@ -147,8 +146,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
147146
geo_file <- cache_path(geo_base_name, ".geojson")
148147
if (!use_cache || !file.exists(geo_file)) {
149148
if (!have_api_key) {
150-
stop(paste("No API key set. Use Sys.setenv(CM_API_KEY = '<your API key>') or",
151-
"options(cancensus.api_key = '<your API key>') to set one."))
149+
stop(paste("No API key set. Use set_api_key('<your API ket>`) to set one, or set_api_key('<your API ket>`, install = TRUE) to save is permanently in our .Renviron."))
152150
}
153151
url <- paste0(base_url, "geo.geojson")
154152
response <- if (!quiet) {
@@ -163,6 +161,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
163161
if (!quiet) message("Reading geo data from local cache.")
164162
}
165163
geos <- geojsonsf::geojson_sf(geo_file) %>%
164+
sf::st_sf() %>% #ust in case
166165
transform_geo(level)
167166

168167
result <- if (is.null(result)) {
@@ -408,6 +407,57 @@ handle_cm_status_code <- function(response,path){
408407
}
409408

410409

410+
name_change_for_level <- function(level){
411+
if (level=='DB') {
412+
name_change <- c('DA_UID'='rpid',
413+
'CSD_UID'='rgid',
414+
'CT_UID'='ruid',
415+
'CMA_UID'='rguid')
416+
} else if (level=='DA'|level=='EA') {
417+
name_change <- c('CSD_UID'='rpid',
418+
'CD_UID'='rgid',
419+
'CT_UID'='ruid',
420+
'CMA_UID'='rguid')
421+
} else if (level=='CT') {
422+
name_change <- c('CMA_UID'='rpid',
423+
'PR_UID'='rgid',
424+
'CSD_UID'='ruid',
425+
'CD_UID'='rguid')
426+
} else if (level=='CSD') {
427+
name_change <- c('CD_UID'='rpid',
428+
'PR_UID'='rgid',
429+
'CMA_UID'='ruid')
430+
} else if (level=='CD') {
431+
name_change <- c('PR_UID'='rpid',
432+
'C_UID'='rgid')
433+
} else if (level=='CMA') {
434+
name_change <- c('PR_UID'='rpid',
435+
'C_UID'='rgid')
436+
} else if (level=='PR') {
437+
name_change <- c('C_UID'='rpid')
438+
} else {
439+
name_change <- c()
440+
warning(paste0("Unknown level ",level))
441+
}
442+
name_change
443+
}
444+
445+
base_name_change <- c("GeoUID"="id",
446+
"Shape Area"="a",
447+
"Type"="t",
448+
"Dwellings"="dw",
449+
"Households"="hh",
450+
"Population"="pop",
451+
"Adjusted Population (previous Census)"="pop2",
452+
"NHS Non-Return Rate"="nrr",
453+
"Quality Flags"="q",
454+
"Population 2011"="pop11",
455+
"Population 2016"="pop16",
456+
"Households 2011"="hh11",
457+
"Households 2016"="hh16",
458+
"Dwellings 2011"="dw11",
459+
"Dwellings 2016"="dw16")
460+
411461
# Transform and rename geometry data.
412462
transform_geo <- function(g, level) {
413463
as_character=c("id","rpid","rgid","ruid","rguid","q")
@@ -416,68 +466,32 @@ transform_geo <- function(g, level) {
416466
as_integer=c("pop","dw","hh","pop2","pop11","pop16","hh11","hh16","dw11","dw16")
417467
#as_character=c(as_character,as_numeric,as_integer)
418468

469+
to_remove <- c("rpid","rgid","ruid","rguid")
470+
to_rename <- base_name_change[as.character(base_name_change) %in% names(g)]
471+
419472
g <- g %>%
420473
dplyr::mutate_at(dplyr::intersect(names(g), as_character), as.character) %>%
421474
dplyr::mutate_at(dplyr::intersect(names(g), as_numeric), as.numeric) %>%
422475
dplyr::mutate_at(dplyr::intersect(names(g), as_integer), as.int) %>%
423-
dplyr::mutate_at(dplyr::intersect(names(g), as_factor), as.factor)
424-
425-
# Change names
426-
# Standard table
427-
name_change <- dplyr::tibble(
428-
old=c("id","a" ,"t" ,"dw","hh","pop","pop2","nrr","q","pop11","pop16","hh11","hh16","dw11","dw16"),
429-
new=c("GeoUID","Shape Area" ,"Type" ,"Dwellings","Households","Population","Adjusted Population (previous Census)","NHS Non-Return Rate","Quality Flags","Population 2011","Population 2016","Households 2011","Households 2016","Dwellings 2011","Dwellings 2016")
430-
)
431-
# Geo UID name changes
432-
if (level=='Regions') {
433-
l=g$t %>% unique()
434-
if (length(l)==1) level=l
435-
}
436-
if (level=='DB') {
437-
name_change <- name_change %>% rbind(
438-
c('rpid','DA_UID'),
439-
c('rgid','CSD_UID'),
440-
c('ruid','CT_UID'),
441-
c('rguid','CMA_UID'))
476+
dplyr::mutate_at(dplyr::intersect(names(g), as_factor), as.factor) %>%
477+
dplyr::rename(!!to_rename)
478+
479+
if (level != "Regions") {
480+
rc <- name_change_for_level(level)[as.character(name_change_for_level(level)) %in% names(g)]
481+
if (length(rc)>0) g <- g %>% dplyr::rename(!!!rc)
482+
} else if ("Type" %in% names(g)) {
483+
g <- g$Type %>%
484+
unique %>%
485+
lapply(function(t){
486+
g <- g %>% dplyr::filter(.data$Type==t)
487+
rc <- name_change_for_level(t)[as.character(name_change_for_level(t)) %in% names(g)]
488+
if (length(rc)>0) g <- g %>% dplyr::rename(!!!rc)
489+
g
490+
}) %>%
491+
do.call(rbind,.) %>%
492+
dplyr::select(-dplyr::one_of(to_remove[to_remove %in% names(.)]))
442493
}
443-
if (level=='DA'|level=='EA') {
444-
name_change <- name_change %>% rbind(
445-
c('rpid','CSD_UID'),
446-
c('rgid','CD_UID'),
447-
c('ruid','CT_UID'),
448-
c('rguid','CMA_UID'))
449-
}
450-
if (level=='CT') {
451-
name_change <- name_change %>% rbind(
452-
c('rpid','CMA_UID'),
453-
c('rgid','PR_UID'),
454-
c('ruid','CSD_UID'),
455-
c('rguid','CD_UID'))
456-
}
457-
if (level=='CSD') {
458-
name_change <- name_change %>% rbind(
459-
c('rpid','CD_UID'),
460-
c('rgid','PR_UID'),
461-
c('ruid','CMA_UID'))
462-
}
463-
if (level=='CD') {
464-
name_change <- name_change %>% rbind(c('rpid','PR_UID'),c('rgid','C_UID'))
465-
}
466-
if (level=='CMA') {
467-
name_change <- name_change %>% rbind(c('rpid','PR_UID'),c('rgid','C_UID'))
468-
}
469-
if (level=='PR') {
470-
name_change <- name_change %>% rbind(c('rpid','C_UID'))
471-
}
472-
473-
used_names <- name_change %>%
474-
dplyr::filter(.data$old %in% names(g))
475-
476-
if (nrow(used_names)>0) g <- g %>%
477-
dplyr::rename(!!!setNames(used_names$old,used_names$new))
478494

479-
to_remove <- dplyr::intersect(names(g),c("rpid","rgid","ruid","rguid"))
480-
if (length(to_remove)>0) g <- g %>% dplyr::select(-dplyr::one_of(to_remove))
481495

482496
return(g)
483497
}

R/helpers.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ cancensus_base_url <- function(){
66
url
77
}
88

9+
valid_api_key <- function(api_key){
10+
!is.null(api_key) && is.character(api_key) && substr(api_key,1,13)=="CensusMapper_"
11+
}
12+
913
robust_api_key <- function(api_key){
10-
api_key <- if (is.null(api_key) && nchar(Sys.getenv("CM_API_KEY")) > 1) { Sys.getenv("CM_API_KEY") } else { api_key }
11-
api_key <- if (is.null(api_key) && !is.null(getOption("cancensus.api_key"))) { getOption("cancensus.api_key") } else { api_key }
14+
api_key <- if (!valid_api_key(api_key) && nchar(Sys.getenv("CM_API_KEY")) > 1) { Sys.getenv("CM_API_KEY") } else { api_key }
15+
api_key <- if (!valid_api_key(api_key) && !is.null(getOption("cancensus.api_key"))) { getOption("cancensus.api_key") } else { api_key }
1216
api_key
1317
}
1418

R/intersect_geometry.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ get_intersecting_geometries <- function(dataset, level, geometry, simplified = F
4545
use_cache = TRUE, quiet = FALSE,
4646
api_key=Sys.getenv("CM_API_KEY")) {
4747
api_key <- robust_api_key(api_key)
48-
have_api_key <- !is.null(api_key)
48+
have_api_key <- valid_api_key(api_key)
4949
result <- NULL
5050

5151
if ("sf" %in% class(geometry)) {
@@ -74,8 +74,7 @@ get_intersecting_geometries <- function(dataset, level, geometry, simplified = F
7474

7575
if (!use_cache || !file.exists(data_file)) {
7676
if (!have_api_key) {
77-
stop(paste("No API key set. Use options(cancensus.api_key = 'XXX') or",
78-
"Sys.setenv(CM_API_KEY = 'XXX') to set one."))
77+
stop(paste("No API key set. Use set_api_key('<your API ket>`) to set one, or set_api_key('<your API ket>`, install = TRUE) to save is permanently in our .Renviron."))
7978
}
8079
url <- paste0(cancensus_base_url(),"/api/v1/intersecting_geographies")
8180
body <- list(dataset=dataset,

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ We'd love to feature examples of work or projects that use cancensus.
112112
If you wish to cite cancensus:
113113

114114
von Bergmann, J., Aaron Jacobs, Dmitry Shkolnik (2020). cancensus: R package to
115-
access, retrieve, and work with Canadian Census data and geography. v0.4.0.
115+
access, retrieve, and work with Canadian Census data and geography. v0.4.1.
116116

117117

118118
A BibTeX entry for LaTeX users is
@@ -121,7 +121,7 @@ A BibTeX entry for LaTeX users is
121121
author = {Jens {von Bergmann} and Dmitry Shkolnik and Aaron Jacobs},
122122
title = {cancensus: R package to access, retrieve, and work With Canadian Census data and geography},
123123
year = {2020},
124-
note = {R package version 0.4.0},
124+
note = {R package version 0.4.1},
125125
url = {https://mountainmath.github.io/cancensus/},
126126
}
127127
```

cran-comments.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Update - v0.4.1
2+
3+
- CRAN check NOTES regarding marked UTF-8 strings are understood but use of non-ASCII characters is intentional due to bilingual EN/FR source data and metadata from national statistics agency.
4+
- Fix minor problem where API key wasn't always picked up if not set correctly as environment variable.
5+
- Fix warning when `t` column not present in downloaded data.
6+
17
# Update - v.0.4.0
28

39
- Added `get_intersecting_geometry` function for new CensusMapper endpoint
@@ -6,9 +12,9 @@
612

713
## Update - v.0.3.2
814

9-
- Add functionality for 1996 census and more refined geographies
10-
- Expanded vignettes
11-
- Fix minor bugs flagged by users
15+
- Add functionality for 1996 census and more refined geographies.
16+
- Expanded vignettes.
17+
- Fix minor bugs flagged by users.
1218

1319
## Update - v.0.3.1
1420

docs/404.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/LICENSE-text.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)