mountainMath / cancensus

R wrapper for calling CensusMapper APIs
https://mountainmath.github.io/cancensus/index.html
Other
82 stars 15 forks source link

"cannot open the connection" errors #110

Closed rgwood closed 6 years ago

rgwood commented 6 years ago

I followed the cancensus installation instructions but was having some trouble running the example commands:

Querying CensusMapper API for regions data...
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'name,geo_uid,type,population,flag,CMA_UID,CD_UID,PR_UID
Canada,01,C,35151728,,,,
Ontario,35,PR,13448494,,,,
Quebec,24,PR,8164361,,,,
+                           vectors=c("v_CA16_408","v_CA16_409","v_CA16_410"),
+                           level='CSD', use_cache = FALSE, geo_format = NA)
Querying CensusMapper API...
Downloading: 1.4 kB     Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
  cannot open file 'GeoUID,Type,Region Name,Area (sq km),Population ,Dwellings ,Households ,v_CA16_408: Occupied private dwellings by structural type of dwelling data,v_CA16_409: Single-detached house,v_CA16_410: Apartment in a building that has five or more storeys
5915001,CSD,Langley (DM),314.76313,117285,43720,41982,41980,21690,1100
5915002,CSD,Langley (CY),10.222,25888,12264,11840,11840,2730,40

These are my sessionInfo() results:

R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
[1] cancensus_0.1.5
loaded via a namespace (and not attached):
 [1] Rcpp_0.12.14     digest_0.6.12    dplyr_0.7.4      withr_2.1.0     
 [5] assertthat_0.2.0 R6_2.2.2         jsonlite_1.5     magrittr_1.5    
 [9] git2r_0.19.0     httr_1.3.1       rlang_0.1.4      curl_3.0        
[13] bindrcpp_0.2     devtools_1.13.4  tools_3.4.3      glue_1.2.0      
[17] compiler_3.4.3   pkgconfig_2.0.1  memoise_1.1.0    bindr_0.1       
[21] tibble_1.3.4  

There is a resolution suggested by @dshkol – install readr (install.packages('readr')).

dshkol commented 6 years ago

With a bit of testing, I can trace this bug directly to a situation where a user 1) doesn't have the readr package installed and is making an API call for the first time, or makes the call with a forced refresh of the cache. The error comes from this section of the code, which occurs in each of get_census, list_census_regions, and list_census_vectors.

  result <- if (!requireNamespace("readr", quietly = TRUE)) {
      dplyr::as_data_frame(utils::read.csv(content, stringsAsFactors = FALSE))
    } else {
      readr::read_csv(content)
    }

If readr is not installed it tries to use the base function read.csv to open the content; however, read.csv is for opening a file connection. In this case, there is no file connection to be made as content is just a string of text. read_csv from readr automatically establishes this text connection step.

There's two ways of fixing this.

1) Establish a text connection to the content string like this:

  result <- if (!requireNamespace("readr", quietly = TRUE)) {
      dplyr::as_data_frame(utils::read.csv(textConnection(content), stringsAsFactors = FALSE))
    } else {
      readr::read_csv(content)
    }

or

2) Make readr a hard dependency and use just that.

Either way works. I don't mind making readr a hard dependency because we already have dplyr as one, and most people who use that likely already have readr installed as a package as well. If we want to maintain the current setup, the fix is easy to implement it and I can add that in, but open to suggestions @mountainMath @atheriel

mountainMath commented 6 years ago

Thanks, looks like you are on top of things with your pull request. Thanks for flagging this @GRIDSVancouver and thanks for the patch @dshkol.

dshkol commented 6 years ago

Fixed in #111