Skip to content

Commit 07567a4

Browse files
authored
Merge pull request #183 from mountainMath/v0.5.1
V0.5.1
2 parents ff69746 + 0f7035b commit 07567a4

File tree

101 files changed

+6898
-2082
lines changed

Some content is hidden

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

101 files changed

+6898
-2082
lines changed

.Rbuildignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ R/hex_sticker.R
1414
images
1515
images/*
1616
card_fix.sh
17+
lastMiKTeXException
1718
^_pkgdown\.yml$
1819
^CRAN-RELEASE$
1920
^\.github$
@@ -22,3 +23,7 @@ card_fix.sh
2223
^\.renviron
2324
^\.rprofile
2425

26+
^doc$
27+
28+
^R/geo_suite.R
29+
^CRAN-SUBMISSION$

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

Lines changed: 17 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
# NOTE: This workflow is overkill for most R packages
2-
# check-standard.yaml is likely a better choice
3-
# usethis::use_github_action("check-standard") will install it.
4-
#
5-
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
6-
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
73
on:
84
push:
9-
branches:
10-
- main
11-
- master
5+
branches: [main, master]
126
pull_request:
13-
branches:
14-
- main
15-
- master
7+
branches: [main, master]
168
schedule:
179
- cron: "15 15 * * *"
1810

@@ -30,100 +22,32 @@ jobs:
3022
config:
3123
- {os: macOS-latest, r: 'release'}
3224
- {os: windows-latest, r: 'release'}
33-
- {os: windows-latest, r: '3.6'}
34-
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
35-
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
36-
# - {os: ubuntu-16.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest", http-user-agent: "R/4.0.0 (ubuntu-16.04) R (4.0.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
37-
# - {os: ubuntu-16.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
38-
# - {os: ubuntu-16.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
39-
# - {os: ubuntu-16.04, r: '3.5', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
40-
#- {os: ubuntu-16.04, r: '3.4', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
41-
#- {os: ubuntu-16.04, r: '3.3', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"}
25+
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
26+
- {os: ubuntu-latest, r: 'release'}
27+
- {os: ubuntu-latest, r: 'oldrel-1'}
4228

4329
env:
44-
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
45-
RSPM: ${{ matrix.config.rspm }}
4630
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
4731
CM_API_KEY: ${{ secrets.CM_API_KEY }}
4832
COMPILE_VIG: ${{ secrets.COMPILE_VIG }}
33+
R_KEEP_PKG_SOURCE: yes
4934

5035
steps:
5136
- uses: actions/checkout@v2
5237

53-
- uses: r-lib/actions/setup-r@v1
38+
- uses: r-lib/actions/setup-pandoc@v2
39+
40+
- uses: r-lib/actions/setup-r@v2
5441
with:
5542
r-version: ${{ matrix.config.r }}
5643
http-user-agent: ${{ matrix.config.http-user-agent }}
44+
use-public-rspm: true
5745

58-
- uses: r-lib/actions/setup-pandoc@v1
59-
- name: "[Custom block] [macOS] Install libraries"
60-
if: runner.os == 'macOS'
61-
run: |
62-
# conflicts with gfortran from r-lib/actions when linking gcc
63-
# rm '/usr/local/bin/gfortran'
64-
brew install pkg-config gdal proj geos udunits
65-
# brew install pkg-config gdal proj geos udunits
66-
67-
- name: Query dependencies
68-
run: |
69-
install.packages('remotes')
70-
saveRDS(remotes::dev_package_deps(dependencies = TRUE, type = "binary"), ".github/depends.Rds", version = 2)
71-
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
72-
shell: Rscript {0}
73-
74-
- name: Cache R packages
75-
if: runner.os != 'Windows'
76-
uses: actions/cache@v2
46+
- uses: r-lib/actions/setup-r-dependencies@v2
7747
with:
78-
path: ${{ env.R_LIBS_USER }}
79-
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-2-${{ hashFiles('.github/depends.Rds') }}
80-
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-2-
81-
82-
- name: Install system dependencies
83-
if: runner.os == 'Linux'
84-
run: |
85-
while read -r cmd
86-
do
87-
eval sudo $cmd
88-
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
89-
90-
- name: Install dependencies on Windows
91-
if: runner.os == 'Windows'
92-
run: |
93-
remotes::install_deps(dependencies = TRUE, type = "binary")
94-
remotes::install_cran("lwgeom", dependencies = TRUE, type="binary")
95-
remotes::install_cran("rcmdcheck")
96-
shell: Rscript {0}
97-
98-
- name: Install dependencies on not Windows
99-
if: runner.os != 'Windows'
100-
run: |
101-
remotes::install_deps(dependencies = TRUE)
102-
remotes::install_cran("rcmdcheck")
103-
remotes::install_cran("sf", force=TRUE)
104-
shell: Rscript {0}
105-
106-
- name: Session info
107-
run: |
108-
options(width = 100)
109-
pkgs <- installed.packages()[, "Package"]
110-
sessioninfo::session_info(pkgs, include_base = TRUE)
111-
shell: Rscript {0}
112-
113-
- name: Check
114-
env:
115-
_R_CHECK_CRAN_INCOMING_: false
116-
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
117-
shell: Rscript {0}
118-
119-
- name: Show testthat output
120-
if: always()
121-
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
122-
shell: bash
48+
extra-packages: any::rcmdcheck
49+
needs: check
12350

124-
- name: Upload check results
125-
if: failure()
126-
uses: actions/upload-artifact@main
51+
- uses: r-lib/actions/check-r-package@v2
12752
with:
128-
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
129-
path: check
53+
upload-snapshots: true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ Meta
1414
CRAN-RELEASE
1515
CRAN-SUBMISSION
1616
.DS_Store
17+
/doc/
18+
/Meta/

DESCRIPTION

Lines changed: 2 additions & 2 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.0
4+
Version: 0.5.3
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"),
@@ -24,7 +24,7 @@ Imports: digest (>= 0.1),
2424
httr (>= 1.0.0),
2525
jsonlite (>= 1.0),
2626
rlang
27-
RoxygenNote: 7.1.2
27+
RoxygenNote: 7.2.1
2828
Suggests: knitr,
2929
ggplot2,
3030
leaflet,

NAMESPACE

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ export(list_cancensus_cache)
1515
export(list_census_datasets)
1616
export(list_census_regions)
1717
export(list_census_vectors)
18+
export(list_recalled_cached_data)
1819
export(parent_census_vectors)
1920
export(remove_from_cancensus_cache)
21+
export(remove_recalled_cached_data)
2022
export(search_census_regions)
2123
export(search_census_vectors)
22-
export(set_api_key)
23-
export(set_cache_path)
24-
export(show_api_key)
25-
export(show_cache_path)
24+
export(set_cancensus_api_key)
25+
export(set_cancensus_cache_path)
26+
export(show_cancensus_api_key)
27+
export(show_cancensus_cache_path)
2628
importFrom(dplyr,"%>%")
2729
importFrom(rlang,.data)
2830
importFrom(stats,setNames)

NEWS.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# cancensus - 0.5.3
2+
- 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 stopping with an error.
3+
- fixes a bug in the local data recall check
4+
5+
# cancensus - 0.5.2
6+
- resolved broken and relocated links picked up in CRAN pretesting
7+
8+
# cancensus - 0.5.1
9+
- Added functionality for users to detect and remove locally cached data that has been recalled by Statistics Canada
10+
- added a check on initial `get_census` call that produces a warning if locally cached data has been recalled by Statistics Canada
11+
- added option to retrieve higher-resolution geometries in `get_census`
12+
13+
## Minor changes
14+
- renamed get/set cache/api_key functions to avoid name conflicts with related packages
15+
116
# cancensus - 0.5.0
217

318
## Minor changes
@@ -81,7 +96,7 @@
8196

8297
## Major changes
8398
- Added a `max_leaves_option` for the `search_census_vectors` function. Adds functionality to set maximum depth for child census vectors, i.e. `max_level=NA` as an additional parameter. Then e.g. `max_level=1` would only get direct children and no grandchildren.
84-
- Resolved issues with mislabelled CD UID for CSD level data
99+
- Resolved issues with mislabeled CD UID for CSD level data
85100
- Improve reliability of character/numeric alignments for geographic UIDS
86101
- `get_census_geography` is now soft deprecated and rolled into the standard `get_census` with parameters
87102

R/cancensus.R

Lines changed: 46 additions & 14 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,26 @@
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+
# Check region selection validity
5566
if (is.na(level)) level="Regions"
5667

68+
# Check spatial resolution
69+
if (is.na(geo_format) && !(resolution %in% c("simplified","high"))) stop("The resolution paramerter needs to be either 'simplified' or 'high'.")
70+
5771
# Turn the region list into a valid JSON dictionary.
5872
if (is.character(regions)) {
5973
if (!quiet) warning(paste("Passing `regions` as a character vector is",
@@ -65,7 +79,6 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
6579
regions <- jsonlite::toJSON(lapply(regions,as.character)) # cast to character in case regions are supplied as numeric/interger
6680
}
6781

68-
6982
# Check if the aggregation level is valid.
7083
if (!level %in% VALID_LEVELS) {
7184
stop("the `level` parameter must be one of 'Regions', 'PR', 'CMA', 'CD', 'CSD', 'CT', 'DA', 'EA' or 'DB'")
@@ -76,7 +89,18 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
7689
} else if (!geo_format %in% c("sf", "sp")) {
7790
stop("the `geo_format` parameter must be one of 'sf', 'sp', or NA")
7891
} else if (!is.na(geo_format) && !requireNamespace("sf", quietly = TRUE)) {
79-
stop("the `sf` package is required to return geographies.")
92+
stop("The `sf` package is required to return geographies.")
93+
}
94+
95+
# Check if SF is installed when asking for spatial data
96+
if(geo_format == "sf" && !("sf" %in% utils::installed.packages())) {
97+
if (utils::menu(c("Yes", "No"),
98+
title= paste("The `sf` package is required to return geographies. Would you like to install?")) == "1") {
99+
utils::install.packages('sf')
100+
} else {
101+
print("Cancelling installation and retrieving tabular data only.")
102+
geo_format <- NA
103+
}
80104
}
81105

82106
base_url=paste0(cancensus_base_url(),"/api/v1/")
@@ -118,6 +142,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
118142
readr::read_csv(na = cancensus_na_strings,
119143
col_types = list(.default = "c"))
120144
} else {
145+
check_recalled_data_and_warn(meta_file,params)
121146
httr::content(response, type = "text", encoding = "UTF-8") %>%
122147
textConnection %>%
123148
utils::read.csv(colClasses = "character", stringsAsFactors = FALSE, check.names = FALSE) %>%
@@ -133,7 +158,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
133158
attr(result, "last_updated") <- Sys.time()
134159
save(result, file = data_file)
135160
file_info <- file.info(data_file)
136-
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(jsonlite::toJSON(regions)),
161+
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),
137162
level=level,
138163
vectors=jsonlite::toJSON(as.character(vectors)) %>% as.character(),
139164
created_at=Sys.time(),
@@ -144,16 +169,19 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
144169
# Load `result` object from cache.
145170
load(file = data_file)
146171
}
147-
touch_metadata(meta_file)
172+
touch_metadata(meta_file,params)
148173
}
149174

150175
if (!is.na(geo_format)) {
151176
params <- list(regions=regions,
152177
level=level,
153178
dataset=dataset,
179+
resolution=resolution,
154180
api_key=api_key)
155-
param_string <- paste0("regions=", regions, "&level=", level, "&dataset=",
156-
dataset)
181+
param_string <- paste0("regions=", regions,
182+
"&level=", level,
183+
"&dataset=", dataset)
184+
if (resolution !="simplified") param_string <- paste0(param_string,"&resolution=",resolution)
157185
geo_base_name <- paste0("CM_geo_", digest::digest(param_string, algo = "md5"))
158186
geo_file <- cache_path(geo_base_name, ".geojson")
159187
meta_file <- paste0(geo_file, ".meta")
@@ -172,11 +200,14 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
172200
geo_version <- response$headers$`data-version`
173201
write(httr::content(response, type = "text", encoding = "UTF-8"), file = geo_file) # cache result
174202
file_info <- file.info(geo_file)
175-
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),level=level,created_at=Sys.time(),
176-
version=data_version,size=file_info$size)
203+
metadata <- dplyr::tibble(dataset=dataset,regions=as.character(regions),
204+
level=level,created_at=Sys.time(),
205+
resolution=resolution,
206+
version=geo_version,size=file_info$size)
177207
saveRDS(metadata, file = meta_file)
178208
} else {
179209
if (!quiet) message("Reading geo data from local cache.")
210+
check_recalled_data_and_warn(meta_file,params)
180211
}
181212
geos <- geojsonsf::geojson_sf(geo_file) %>%
182213
sf::st_sf(agr="constant") %>% # need to set this, otherwise sf complains
@@ -192,7 +223,7 @@ get_census <- function (dataset, regions, level=NA, vectors=c(), geo_format = NA
192223
dplyr::select(result, -dplyr::one_of(to_remove)) %>%
193224
dplyr::inner_join(geos, ., by = "GeoUID")
194225
}
195-
touch_metadata(meta_file)
226+
touch_metadata(meta_file,params)
196227
}
197228

198229
# ensure sf format even if library not loaded and set agr columns
@@ -268,7 +299,7 @@ VALID_LEVELS <- c("Regions","C","PR", "CMA", "CD", "CSD", "ADA","CT", "DA", 'EA'
268299

269300
#' Query the CensusMapper API for available datasets.
270301
#'
271-
#' @param use_cache If set to TRUE (the dfault), data will be read from a temporary local cache for the
302+
#' @param use_cache If set to TRUE (the default), data will be read from a temporary local cache for the
272303
#' duration of the R session, if
273304
#' available. If set to FALSE, query the API for the data, and
274305
#' refresh the temporary cache with the result.
@@ -279,7 +310,7 @@ VALID_LEVELS <- c("Regions","C","PR", "CMA", "CD", "CSD", "ADA","CT", "DA", 'EA'
279310
#' Returns a data frame with a column \code{dataset} containing the code for the
280311
#' dataset, a column \code{description} describing it, a \code{geo_dataset} column
281312
#' identifying the geography dataset the data is based on, a \code{attribution} column
282-
#' with an attribtuion string, a \code{reference} column with a reference identifier, and
313+
#' with an attribution string, a \code{reference} column with a reference identifier, and
283314
#' a \code{reference_url} column with a link to reference materials.
284315
#'
285316
#' @export
@@ -400,6 +431,7 @@ label_vectors <- function(x) {
400431
#' # Get details for truncated vectors:
401432
#' census_vectors(census_data)
402433
#' }
434+
#' @keywords internal
403435
#' @export
404436
census_vectors <- function(x) {
405437
warning("census_vectors() is deprecated. Please use label_vectors() to view details for truncated variable labels.")

0 commit comments

Comments
 (0)