Skip to content

Commit af9ec76

Browse files
committed
roll back caa86c8 and add change for spatial format checking back in.
1 parent ac85ada commit af9ec76

23 files changed

Lines changed: 113 additions & 52 deletions

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# cancensus - 0.5.4
2-
- add a convenience function for creating unique names within given selection of regions from `list_census_regions()`
2+
- added ability to query census datasets by year
3+
- added a convenience function for creating unique names within given selection of regions from `list_census_regions()`
34
- added a check and context menu to install `sf` package when user requests spatial data but does not have the required package installed as opposed to erroring out.
45
- improved checking that correct spatial formats are requested
56
- preparing for 'sp' spatial format usage deprecation in future versions

R/cancensus.R

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
#' @param regions A named list of census regions to retrieve. Names must be valid census aggregation levels.
1212
#' @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).
1313
#' @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.
14-
#' @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).
14+
#' @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.
15+
#' @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.
16+
#' Simplification generally increases as the geographic aggregation level increases.
17+
#' If high resolution geometries are required
18+
#' then this option can be set to 'high'. By default this setting is set to \code{'simplified'}.
1519
#' @param labels Set to "detailed" by default, but truncated Census variable names can be selected by setting labels = "short". Use \code{label_vectors(...)} to return variable label information in detail.
1620
#' @param use_cache If set to TRUE (the default) data will be read from the local cache if available.
1721
#' @param quiet When TRUE, suppress messages and warnings.
@@ -44,16 +48,28 @@
4448
#' # Get details for truncated vectors:
4549
#' label_vectors(census_data)
4650
#'}
47-
get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA, labels = "detailed",
48-
use_cache=TRUE, quiet=FALSE, api_key=Sys.getenv("CM_API_KEY")) {
51+
get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA,
52+
resolution = 'simplified',
53+
labels = "detailed",
54+
use_cache=TRUE, quiet=FALSE, api_key=Sys.getenv("CM_API_KEY"))
55+
{
56+
57+
# API and data recall checks
58+
first_run_checks()
4959
api_key <- robust_api_key(api_key)
5060
have_api_key <- valid_api_key(api_key)
5161
result <- NULL
5262
data_version<-NULL
5363
geo_version<-NULL
5464

65+
dataset <- translate_dataset(dataset)
66+
67+
# Check region selection validity
5568
if (is.na(level)) level="Regions"
5669

70+
# Check spatial resolution
71+
if (is.na(geo_format) && !(resolution %in% c("simplified","high"))) stop("The resolution paramerter needs to be either 'simplified' or 'high'.")
72+
5773
# Turn the region list into a valid JSON dictionary.
5874
if (is.character(regions)) {
5975
if (!quiet) warning(paste("Passing `regions` as a character vector is",
@@ -65,17 +81,24 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
6581
regions <- jsonlite::toJSON(lapply(regions,as.character)) # cast to character in case regions are supplied as numeric/interger
6682
}
6783

68-
6984
# Check if the aggregation level is valid.
7085
if (!level %in% VALID_LEVELS) {
7186
stop("the `level` parameter must be one of 'Regions', 'PR', 'CMA', 'CD', 'CSD', 'CT', 'DA', 'EA' or 'DB'")
7287
}
7388

89+
# Check that we can read the requested geo format.
90+
if (is.na(geo_format)) { # This is ok.
91+
} else if (!geo_format %in% c("sf", "sp")) {
92+
stop("the `geo_format` parameter must be one of 'sf', 'sp', or NA")
93+
} else if (!is.na(geo_format) && !requireNamespace("sf", quietly = TRUE)) {
94+
stop("The `sf` package is required to return geographies.")
95+
}
96+
7497
# --------- Spatial format checks --------------------------------------------------------------------#
7598
# This section checks that proper spatial formats are requested. If users select spatial data and
7699
# don't have the 'sf' package installed, will prompt them with a menu to install it, otherwise we will
77100
# return spatial data only. If users select 'sp' format, will advise them that usage is deprecated and nudge
78-
# to install 'sf' pacakge.
101+
# to install 'sf' package.
79102
if (!is.na(geo_format)) {
80103
if(!geo_format %in% c("sf","sp")) {
81104
stop("the `geo_format` parameter should be 'sf', 'sp', or NA")
@@ -138,6 +161,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
138161
readr::read_csv(na = cancensus_na_strings,
139162
col_types = list(.default = "c"))
140163
} else {
164+
check_recalled_data_and_warn(meta_file,params)
141165
httr::content(response, type = "text", encoding = "UTF-8") %>%
142166
textConnection %>%
143167
utils::read.csv(colClasses = "character", stringsAsFactors = FALSE, check.names = FALSE) %>%
@@ -153,7 +177,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
153177
attr(result, "last_updated") <- Sys.time()
154178
save(result, file = data_file)
155179
file_info <- file.info(data_file)
156-
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(jsonlite::toJSON(regions)),
180+
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),
157181
level=level,
158182
vectors=jsonlite::toJSON(as.character(vectors)) %>% as.character(),
159183
created_at=Sys.time(),
@@ -164,16 +188,19 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
164188
# Load `result` object from cache.
165189
load(file = data_file)
166190
}
167-
touch_metadata(meta_file)
191+
touch_metadata(meta_file,params)
168192
}
169193

170194
if (!is.na(geo_format)) {
171195
params <- list(regions=regions,
172196
level=level,
173197
dataset=dataset,
198+
resolution=resolution,
174199
api_key=api_key)
175-
param_string <- paste0("regions=", regions, "&level=", level, "&dataset=",
176-
dataset)
200+
param_string <- paste0("regions=", regions,
201+
"&level=", level,
202+
"&dataset=", dataset)
203+
if (resolution !="simplified") param_string <- paste0(param_string,"&resolution=",resolution)
177204
geo_base_name <- paste0("CM_geo_", digest::digest(param_string, algo = "md5"))
178205
geo_file <- cache_path(geo_base_name, ".geojson")
179206
meta_file <- paste0(geo_file, ".meta")
@@ -192,11 +219,14 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
192219
geo_version <- response$headers$`data-version`
193220
write(httr::content(response, type = "text", encoding = "UTF-8"), file = geo_file) # cache result
194221
file_info <- file.info(geo_file)
195-
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),level=level,created_at=Sys.time(),
196-
version=data_version,size=file_info$size)
222+
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),
223+
level=level,created_at=Sys.time(),
224+
resolution=resolution,
225+
version=geo_version,size=file_info$size)
197226
saveRDS(metadata, file = meta_file)
198227
} else {
199228
if (!quiet) message("Reading geo data from local cache.")
229+
check_recalled_data_and_warn(meta_file,params)
200230
}
201231
geos <- geojsonsf::geojson_sf(geo_file) %>%
202232
sf::st_sf(agr="constant") %>% # need to set this, otherwise sf complains
@@ -212,7 +242,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
212242
dplyr::select(result, -dplyr::one_of(to_remove)) %>%
213243
dplyr::inner_join(geos, ., by = "GeoUID")
214244
}
215-
touch_metadata(meta_file)
245+
touch_metadata(meta_file,params)
216246
}
217247

218248
# ensure sf format even if library not loaded and set agr columns
@@ -299,7 +329,7 @@ VALID_LEVELS <- c("Regions","C","PR", "CMA", "CD", "CSD", "ADA","CT", "DA", 'EA'
299329
#' Returns a data frame with a column \code{dataset} containing the code for the
300330
#' dataset, a column \code{description} describing it, a \code{geo_dataset} column
301331
#' identifying the geography dataset the data is based on, a \code{attribution} column
302-
#' with an attribtuion string, a \code{reference} column with a reference identifier, and
332+
#' with an attribution string, a \code{reference} column with a reference identifier, and
303333
#' a \code{reference_url} column with a link to reference materials.
304334
#'
305335
#' @export
@@ -349,6 +379,7 @@ list_census_datasets <- function(use_cache = TRUE, quiet = FALSE) {
349379
#' # Attribution string for the 2006 and 2016 census datasets
350380
#' dataset_attribution(c('CA06','CA16'))
351381
dataset_attribution <- function(datasets){
382+
datasets <- lapply(datasets,translate_dataset) %>% unlist()
352383
attribution <-list_census_datasets(quiet=TRUE) %>%
353384
dplyr::filter(.data$dataset %in% datasets) %>%
354385
dplyr::pull(.data$attribution)
@@ -420,6 +451,7 @@ label_vectors <- function(x) {
420451
#' # Get details for truncated vectors:
421452
#' census_vectors(census_data)
422453
#' }
454+
#' @keywords internal
423455
#' @export
424456
census_vectors <- function(x) {
425457
warning("census_vectors() is deprecated. Please use label_vectors() to view details for truncated variable labels.")

cran-comments.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Update - 0.5.4
2+
- added ability to query census datasets by year
23
- add a convenience function for creating unique names within given selection of regions from `list_census_regions()`
34
- added a check and context menu to install `sf` package when user requests spatial data but does not have the required package installed as opposed to erroring out.
45
- improved checking that correct spatial formats are requested

docs/articles/Making_maps_with_cancensus.html

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/cancensus.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/data_discovery.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1 Byte
Loading
-1.56 KB
Loading
-991 Bytes
Loading
-991 Bytes
Loading

0 commit comments

Comments
 (0)