Skip to content

Commit

Permalink
version 0.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mountainMath authored and cran-robot committed Oct 6, 2023
1 parent a7fd9ec commit e90b0fc
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 207 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: cmhc
Type: Package
Title: Access, Retrieve, and Work with CMHC Data
Version: 0.2.5
Version: 0.2.6
Author: Jens von Bergmann
Maintainer: Jens von Bergmann <jens@mountainmath.ca>
Description: Wrapper around the Canadian Mortgage and Housing Corporation (CMHC) web interface. It enables programmatic and reproducible access to a wide variety of housing data from CMHC.
Expand All @@ -20,6 +20,6 @@ URL: https://github.com/mountainMath/cmhc,
https://www03.cmhc-schl.gc.ca/hmip-pimh/en
BugReports: https://github.com/mountainMath/cmhc/issues
RoxygenNote: 7.2.3
Packaged: 2023-04-02 22:13:13 UTC; jens
Packaged: 2023-10-06 20:21:36 UTC; jens
Repository: CRAN
Date/Publication: 2023-04-02 22:30:19 UTC
Date/Publication: 2023-10-06 20:40:10 UTC
24 changes: 12 additions & 12 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
3b27472f5cb6d5d2f7bb5f0427a704b5 *DESCRIPTION
e84512b599f0db455e191826618c05aa *DESCRIPTION
a769d83051d3bbbd54bdb7850f8bb5ff *LICENSE
c443651a13d475596dbb05de3860d7df *NAMESPACE
2ebaf223fb7b546e1950516652a7e00b *NEWS.md
3dca85cd83e568e590bc9cd718f6d045 *R/cmhc.R
6b07eb535434721c5f10660faa32ca0b *R/cmhc_geography.R
21018e8512d5deacd0e1e08722907f80 *R/cmhc_tables.R
9574c3adc1b36c7b2ee21eb1aff975c7 *NEWS.md
c206a91bfcab290a1d108a6509ce6b78 *R/cmhc.R
ee4f0ef851b1c343e4e632aa86774222 *R/cmhc_geography.R
cf15c75f33c904caa9407688a12a4ac8 *R/cmhc_tables.R
10e31c2764df4f2c5c430dac1bc8302b *R/helpers.R
1be87bfe8b868a2c3227129a89ca3aa2 *R/user_settings.R
2f0cb42245820b318e2fa956f4221469 *README.md
2b36388411f0d721c96ce27ef3ec83b7 *build/vignette.rds
4faf5a9b2cea339df884940d7d299c70 *README.md
1be387320707aa0a4c6746a53b73b950 *build/vignette.rds
9b1c83b427fab38dd3d612fb632b75ea *data/cmhc_cma_translation_data.rda
4375065d7e8317bcd7354cfa7917dd5e *data/cmhc_csd_translation_data.rda
8d602f080df477a7515275328e8bb8df *data/cmhc_ct_translation_data.rda
b915037896d3ffccd023c1c67a99c611 *inst/doc/basic_usage.R
6c67536bcca6c6aa48c56978580e8b9b *inst/doc/basic_usage.R
f478f138819a7300535d501307873840 *inst/doc/basic_usage.Rmd
8d68df3fc7c43a760d935d95523a91df *inst/doc/basic_usage.html
ab14b470124a4fb4bfd0afe9eaad9e32 *inst/doc/rental_universe.R
d71f927da7753c6280048f50f4acd9b6 *inst/doc/basic_usage.html
f8c20d65b96bc98eb5aec60f0cbc883e *inst/doc/rental_universe.R
c61323aaf00e65296e9079108b00e670 *inst/doc/rental_universe.Rmd
5bb246fe9e0f8dabf6f8a179b86d987a *inst/doc/rental_universe.html
07263abbf3bd1e12ec5a31f45fce78f7 *inst/doc/rental_universe.html
7f764736a449069cf106e74390e50f85 *man/cmhc_cma_translation_data.Rd
1c76d070418aa1b45e200d4ff8ca4638 *man/cmhc_csd_translation_data.Rd
bfce4db6c7089841ecfafa4513bae1dd *man/cmhc_ct_translation_data.Rd
c922b26c4dd4064825bbe504b2749977 *man/cmhc_quality_labels.Rd
9767289d02e6e2d55df56aab46a5ace7 *man/figures/logo.png
b1d7294212069eeb2887f29db0dc03b5 *man/get_cmhc.Rd
91d543d1771948c07f3cbf017c4230a5 *man/get_cmhc.Rd
c0f524a94df47c0ba49bec6e23d92ace *man/get_cmhc_geography.Rd
2e55b1b0a85b7d54e2e3709c30915caf *man/list_cmhc_breakdowns.Rd
28b576aa1ce6dc97d98befba67959346 *man/list_cmhc_dimensions.Rd
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@
# cmhc v0.2.5

* conditional check of vignettes to account for CMHC server issues

# cmhc v0.2.6

* add tables
* adjust to changes with CMHC portal that require MetId to be specified (only partial fix at this point)
* fix link to CMHC terms of use that moved

13 changes: 10 additions & 3 deletions R/cmhc.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#' @param month optional, only needed when querying data for a snapshot in time.
#' @param frequency optional, only needed when querying time series data, one of "Monthly", "Quarterly" or "Annual".
#' @param filters optional list of filters, consult `list_cmhc_filters()` for possible values.
#' @param refresh optional, refresh the temporary local cache of the CMHC tables. Defaults to `FALSE`.
#'
#' @return A tibble with the data in long form.
#'
Expand All @@ -41,7 +42,8 @@
get_cmhc <- function(survey,series, dimension, breakdown,geoFilter="Default",
geo_uid,
year=NULL, month=NULL, frequency = NULL,
filters=list()){
filters=list(),
refresh=FALSE){
table_list <- list_cmhc_tables(short=FALSE)
if (!is.null(frequency)) {
frequency <- stringr::str_to_title(frequency)
Expand Down Expand Up @@ -105,7 +107,7 @@ get_cmhc <- function(survey,series, dimension, breakdown,geoFilter="Default",

geo_names <- names(geo_uid)
if (is.null(geo_names)) {
region_params <- cmhc_region_params_from_census(geo_uid)
region_params <- cmhc_region_params_from_census(geo_uid)
} else {
if (c("Neighbourhood") %in% geo_names|c("Hood") %in% geo_names) {
nid <- geo_uid["Neighbourhood"] |> as.character()
Expand All @@ -120,6 +122,8 @@ get_cmhc <- function(survey,series, dimension, breakdown,geoFilter="Default",
}
region_params$geography_type_id="6"
region_params$geography_id=hood$METNBHD
} else {
stop(paste0("Unknown regions: ",region_params))
}
}

Expand All @@ -136,6 +140,9 @@ get_cmhc <- function(survey,series, dimension, breakdown,geoFilter="Default",
#ForTimePeriod.Year=year,
exportType="csv"
)
if (!is.null(region_params$MetId)) {
query_params$MetId=region_params$MetId
}
if (!is.null(year)) {
query_params$ForTimePeriod.Year=year
}
Expand All @@ -156,7 +163,7 @@ get_cmhc <- function(survey,series, dimension, breakdown,geoFilter="Default",

filehash <- digest::digest(query_params)
data_file=file.path(tempdir(),paste0("cmhc_",filehash,".csv"))
if (!file.exists(data_file)) {
if (refresh||!file.exists(data_file)) {
url="https://www03.cmhc-schl.gc.ca/hmip-pimh/en/TableMapChart/ExportTable"
cookie='ORDERDESKSID=jFINZMyDxkcEQBY3IJL4p2tWB0kFbPOXLJC7Fv4uVCdYBCNcqIUgi7N53swo1Qty; WT_FPC=id=66.183.109.243-320627712.30508028:lv=1466113349996:ss=1466113349996; BIGipServerpool-HMIP-PROD=rd22o00000000000000000000ffff0a009815o80; _ga=GA1.3.64898709.1458624685; DoNotShowIntro=true; _ga=GA1.4.64898709.1458624685; ORDERDESKSID=cCfzb1jZknrSTdfE1Db8rxWifrIuRL9BGT4ae8kd5xDATcXjkfkVDDDuTn6Fxhgl; LUI=; AUTOLOGINTOKEN='
share_token="L2htaXAtcGltaC9lbi9UYWJsZU1hcENoYXJ0L1RhYmxlP1RhYmxlSWQ9MS4xLjIuOSZHZW9ncmFwaHlJZD0yNDEwJkdlb2dyYXBoeVR5cGVJZD0zJkJyZWFrZG93bkdlb2dyYXBoeVR5cGVJZD00JkRpc3BsYXlBcz1UYWJsZSZHZW9ncmFnaHlOYW1lPVZhbmNvdXZlciZZdGQ9RmFsc2UmRGVmYXVsdERhdGFGaWVsZD1tZWFzdXJlLTExJlN1cnZleT1TY3NzJkZvclRpbWVQZXJpb2QuWWVhcj0yMDE2JkZvclRpbWVQZXJpb2QuUXVhcnRlcj0zJkZvclRpbWVQZXJpb2QuTW9udGg9OA%253D%253D"
Expand Down
50 changes: 48 additions & 2 deletions R/cmhc_geography.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ cmhc_region_params_from_census <- function(GeoUID){
cmhc_geography_type_list=list(C=1,PR=2,CMA=3, CSD=4, CT=7, ZONE=5,NEIGHBOUHOOD=6)
list <- list("geography_type_id"=as.character(cmhc_geography_type_list[cmhc_geo_level_for_census(GeoUID)]),
"geography_id"=census_to_cmhc_geocode(GeoUID))
metid <- cmhc_met_id_for_census(GeoUID)
if (!is.na(metid)) list$MetId <- metid
return(list)
}

Expand Down Expand Up @@ -68,6 +70,50 @@ census_to_cmhc_geocode <- function(GeoUID){
result
}

#' translate census to CMHC met id, if applicable
#' @param GeoUID Census geographic identifier for CMA or CSD
#' @keywords internal
#' @noRd
#' @return a character string with the CMHC MetId
cmhc_met_id_for_census <- function(GeoUID) {
cmhc_name <- list(
"CT"="CMHCCT_UID",
"CSD"="CMHC_CSDUID",
"CMA"="METCODE"
)
census_name <- list(
"CT"="CTUID",
"CSD"="CSDUID",
"CMA"="CMAUID"
)
geo_level = cmhc_geo_level_for_census(GeoUID)

if (is.null(geo_level)) stop("Could not recognize GeoUID.")

if (geo_level=="CSD") {
cma_id <- cancensus::list_census_regions("2021") %>%
dplyr::filter(.data$region==GeoUID) %>%
dplyr:: pull(.data$CMA_UID)
if (nchar(cma_id)==3) {
pr_uid <- substr(GeoUID,1,2)
cma_id <- paste0(pr_uid,cma_id)
}
cmhc_cma_id <- cmhc::cmhc_cma_translation_data %>%
filter(.data$CMA_UID==cma_id) %>%
pull(.data$METCODE)
}

result <- switch(
geo_level,
"CMA" = cmhc::cmhc_cma_translation_data %>% mutate(CMA_UID=ifelse(nchar(GeoUID)==3 & nchar(.data$CMA_UID)==5,substr(.data$CMA_UID,3,5),.data$CMA_UID)) %>% filter(.data$CMA_UID==GeoUID) %>% pull(.data$METCODE),
"CT" = cmhc::cmhc_ct_translation_data %>% filter(.data$CTUID==GeoUID) %>% pull(.data$METCODE) %>% unique(),
"CSD" = cmhc_cma_id,
"PR" = NA_character_
)

result
}

#' translate CMHC to census geographic identifier
#' @param GeoUID CMHC geographic identifier for CMA or CSD or CT
#' @param parent_region census geographic identifier to identify CMA if needed
Expand Down Expand Up @@ -102,8 +148,8 @@ cmhc_to_census_geocode <- function(GeoUID,parent_region=NULL){
if (parent_geo_level=="CMA") {
CMA_GEOUID <- parent_region
} else if (parent_geo_level=="CSD"){
link <- cmhc::cmhc_ct_translation_data |>
filter(.data$CSDUID==parent_region) |>
link <- cmhc::cmhc_ct_translation_data %>%
filter(.data$CSDUID==parent_region) %>%
select(.data$CTUID,.data$CMHC_CT) %>%
mutate(CMA_UID=substr(.data$CTUID,1,3))
CMA_GEOUID <- unique(link$CMA_UID)
Expand Down
2 changes: 1 addition & 1 deletion R/cmhc_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmhc_dwelling_types <- c("Single","Semi-detached","Row","Apartment","All")
cmhc_intended_markets <- c("Homeowner","Rental","Condo","Co-op","All")
cmhc_type_codes1 <- c("Provinces"=2,"Centres"=3,"Survey Zones"=8,"Census Subdivision"=9,"Neighbourhoods"=10,"Census Tracts"=11)
cmhc_type_codes2 <- c("Survey Zones"=6,"Census Subdivision"=7,"Neighbourhoods"=8,"Census Tracts"=9)
cmhc_type_codes3 <- c("Survey Zones"=3,"Census Subdivision"=4,"Neighbourhoods"=5,"Census Tracts"=6)
cmhc_type_codes3 <- c("Provinces"=1,"Centres"=2,"Survey Zones"=3,"Census Subdivision"=4,"Neighbourhoods"=5,"Census Tracts"=6)
cmhc_type_codes4 <- c("Survey Zones"=3,"Census Subdivision"=4)
cmhc_series_dimension_codes1 <- c("Dwelling Type"=1,"Intended Market"=4)
cmhc_bedroom_types <- c("Bachelor","1 Bedroom","2 Bedroom","3 Bedroom +","Total")
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ Starting with version v.0.3.2 the package has an interactive query builder helpe
If you wish to cite cmhc:

von Bergmann, J. cmhc: R package to
access, retrieve, and work with CMHC data. v0.2.5.
access, retrieve, and work with CMHC data. v0.2.6.


A BibTeX entry for LaTeX users is
```
@Manual{cmhc,
author = {Jens {von Bergmann}},
title = {cmhc: R package to access, retrieve, and work with CMHC data},
year = {2022},
note = {R package version 0.2.5},
year = {2023},
note = {R package version 0.2.6},
url = {https://mountainmath.github.io/cmhc/},
}
```
Expand All @@ -72,7 +72,7 @@ The cmhc package is designed to work well with the [**cancensus** package](https

## CMHC Attribution

Subject to the [CMHC Data License Agreement](https://www.cmhc-schl.gc.ca/en/about-us/terms-conditions/hmip-terms-conditions), licensed products using CMHC data should employ the following acknowledgement of source:
Subject to the [CMHC Data License Agreement](https://www.cmhc-schl.gc.ca/about-us/terms-conditions/hmip-terms-conditions), licensed products using CMHC data should employ the following acknowledgement of source:


### Acknowledgment of Source
Expand Down
Binary file modified build/vignette.rds
Binary file not shown.
2 changes: 1 addition & 1 deletion inst/doc/basic_usage.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ---- include = FALSE---------------------------------------------------------
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
Expand Down
174 changes: 90 additions & 84 deletions inst/doc/basic_usage.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/doc/rental_universe.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ---- include = FALSE---------------------------------------------------------
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
Expand Down

0 comments on commit e90b0fc

Please sign in to comment.