66# '
77# ' @param census_year census year to get the data for, right now only 2021 is supported
88# ' @param level geographic level to return the data for, valid choices are
9- # ' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA"
9+ # ' "PR","CD","CMACA","CSD","CT","ADA","DA","ER","FED","DPL","POPCNTR", "FSA", "HR"
1010# ' @param version optionally specify a version of the data to download. For example, for FED level data, version 1.3 will
1111# ' access the 2013 represenation order, whereas version 2.0 will access the 2023 representation order. By default the latest
1212# ' available version is accessed.
2121# ' @export
2222get_statcan_wds_metadata <- function (census_year ,level ,version = NULL ,refresh = FALSE ){
2323 valid_census_years <- c(" 2021" )
24- valid_levels <- c(" PR" ," CD" ," CMACA" ," CSD" ," CT" ," ADA" ," DA" ," ER" ," FED" ," DPL" ," POPCNTR" ," FSA" )
24+ valid_levels <- c(" PR" ," CD" ," CMACA" ," CSD" ," CT" ," ADA" ," DA" ," ER" ," FED" ," DPL" ," POPCNTR" ," FSA" , " HR " )
2525 if (! (census_year %in% valid_census_years )) {
2626 stop(paste0(" Census year must be one of " ,paste0(valid_census_years ,collapse = " , " )," ." ))
2727 }
@@ -38,7 +38,7 @@ get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALS
3838 d <- xml2 :: read_xml(metadata_tempfile )
3939 code_lists <- xml2 :: xml_find_all(d ," //structure:Codelist" )
4040
41- meta_data <- lapply(code_lists , \ (cl ){
41+ meta_data <- lapply(code_lists , function (cl ){
4242 codelist_id <- cl %> % xml2 :: xml_attr(" id" )
4343 agencyID <- cl %> % xml2 :: xml_attr(" agencyID" )
4444 codelist_en <- cl %> % xml2 :: xml_find_all(" common:Name[@xml:lang='en']" ) %> % xml2 :: xml_text()
@@ -54,7 +54,7 @@ get_statcan_wds_metadata <- function(census_year,level,version=NULL,refresh=FALS
5454 en = codes %> % xml2 :: xml_find_all(" common:Name[@xml:lang='en']" ) %> % xml2 :: xml_text(),
5555 fr = codes %> % xml2 :: xml_find_all(" common:Name[@xml:lang='fr']" ) %> % xml2 :: xml_text(),
5656 `Parent ID` = codes %> % xml2 :: xml_find_all(" structure:Parent/Ref" ,flatten = FALSE ) %> %
57- lapply(\ (d )ifelse(is.null(d ),NA ,xml2 :: xml_attr(d ," id" ))) %> % unlist()
57+ lapply(function (d )ifelse(is.null(d ),NA ,xml2 :: xml_attr(d ," id" ))) %> % unlist()
5858 )
5959 }) %> %
6060 dplyr :: bind_rows()
@@ -118,9 +118,17 @@ get_statcan_wds_data <- function(DGUIDs,
118118 httr :: accept(" text/csv" ),
119119 httr :: add_headers(" Accept-Encoding" = " deflate, gzip, br" ),
120120 httr :: write_disk(wds_data_tempfile ,overwrite = TRUE ))
121- }
122- if (! response $ status_code == " 200" ) {
123- stop(paste0(" Invalid request.\n " ,httr :: content(response )))
121+ if (! response $ status_code == " 200" ) {
122+ if (! is.null(response $ error ) && (" curl_error_peer_failed_verification" %in% class(response $ error ))) {
123+ stop(paste0(strwrap(gsub(" .+\\ ): " ," " ,as.character(response $ error ),80 )),collapse = " \n " )," \n " ,
124+ " This means that the authenticity of the StatCan API server can't be verified.\n " ,
125+ " Statistics Canada has a history of failty SSL certificats on their API,\n " ,
126+ " if you are reasonably sure that your connection is not getting hijacked you\n " ,
127+ " can disable peer checking for the duration of the R session by typing\n\n " ,
128+ " httr::set_config(httr::config(ssl_verifypeer=0,ssl_verifystatus=0))" ," \n\n " ," into the console." )
129+ }
130+ stop(paste0(" Invalid request.\n " ,httr :: content(response )))
131+ }
124132 }
125133 census_year <- " 2021"
126134 meta_data <- get_statcan_wds_metadata(census_year ,level = level ,version = version ,refresh = refresh )
0 commit comments