Skip to content

Commit 7cd2692

Browse files
committed
fix problem with api key and remove funky warning regarding unknown column 't', bump version for update
1 parent 2a930c4 commit 7cd2692

55 files changed

Lines changed: 549 additions & 544 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 problem with not picking up api key if not set 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: 73 additions & 57 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"
@@ -163,6 +163,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
163163
if (!quiet) message("Reading geo data from local cache.")
164164
}
165165
geos <- geojsonsf::geojson_sf(geo_file) %>%
166+
sf::st_sf() %>% #ust in case
166167
transform_geo(level)
167168

168169
result <- if (is.null(result)) {
@@ -408,6 +409,57 @@ handle_cm_status_code <- function(response,path){
408409
}
409410

410411

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

471+
to_remove <- c("rpid","rgid","ruid","rguid")
472+
to_rename <- base_name_change[as.character(base_name_change) %in% names(g)]
473+
419474
g <- g %>%
420475
dplyr::mutate_at(dplyr::intersect(names(g), as_character), as.character) %>%
421476
dplyr::mutate_at(dplyr::intersect(names(g), as_numeric), as.numeric) %>%
422477
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'))
478+
dplyr::mutate_at(dplyr::intersect(names(g), as_factor), as.factor) %>%
479+
dplyr::rename(!!to_rename)
480+
481+
if (level != "Regions") {
482+
rc <- name_change_for_level(level)[as.character(name_change_for_level(level)) %in% names(g)]
483+
if (length(rc)>0) g <- g %>% dplyr::rename(!!!rc)
484+
} else if ("Type" %in% names(g)) {
485+
g <- g$Type %>%
486+
unique %>%
487+
lapply(function(t){
488+
g <- g %>% dplyr::filter(.data$Type==t)
489+
rc <- name_change_for_level(t)[as.character(name_change_for_level(t)) %in% names(g)]
490+
if (length(rc)>0) g <- g %>% dplyr::rename(!!!rc)
491+
g
492+
}) %>%
493+
do.call(rbind,.) %>%
494+
dplyr::select(-dplyr::one_of(to_remove[to_remove %in% names(.)]))
442495
}
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))
478496

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))
481497

482498
return(g)
483499
}

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

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Update - v0.4.1
2+
3+
- Fix problem with not picking up api key if not set as environment variable
4+
- Fix warning when 't' column not present in downloaded data
5+
16
# Update - v.0.4.0
27

38
- Added `get_intersecting_geometry` function for new CensusMapper endpoint

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)