rchoroplethr

state_choropleth not working for >1 state


Trying a very basic state choropleth:

library(choroplethr)
library(choroplethrMaps)
state_choropleth(df_pop_state)

It exits with this error:

Error in private$zoom == "alaska" || private$zoom == "hawaii" : 
  'length = 51' in coercion to 'logical(1)'

which is the same as the one found here, though in that case, the map seems to get produced.

If I zoom to one state, it works fine:

state_choropleth(df_pop_state, zoom=c("washington"))

No idea what's going on. I'm pretty sure everything is up to date. I did have an R upgrade (maybe from version 3 to 4?) a few months ago, so maybe that's related.

TIA.

Results of sessionInfo()

R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Rome
tzcode source: internal

attached base packages:
[1] datasets  utils     stats     graphics  grDevices methods   base     

other attached packages:
 [1] dplyr_1.1.2           zoo_1.8-12            viridis_0.6.3         viridisLite_0.4.2    
 [5] scales_1.2.1          readr_2.1.4           RColorBrewer_1.1-3    plyr_1.8.8           
 [9] plotrix_3.8-2         PBSmapping_2.73.2     maptools_1.1-7        sp_2.0-0             
[13] lubridate_1.9.2       jsonlite_1.8.5        ggmap_3.0.2           ggplot2_3.4.2        
[17] devtools_2.4.5        usethis_2.2.1         deldir_1.0-9          clipr_0.8.0          
[21] choroplethrZip_1.5.0  choroplethrMaps_1.0.1 choroplethr_3.7.1     acs_2.1.4            
[25] XML_3.99-0.14         stringr_1.5.0         car_3.1-2             carData_3.0-5        
[29] calibrate_1.7.7       MASS_7.3-60           alphahull_2.5         datapasta_3.1.1      

loaded via a namespace (and not attached):
  [1] rstudioapi_0.14       magrittr_2.0.3        spatstat.utils_3.0-3  rmarkdown_2.22       
  [5] fs_1.6.2              vctrs_0.6.3           memoise_2.0.1         base64enc_0.1-3      
  [9] htmltools_0.5.5       Formula_1.2-5         KernSmooth_2.23-21    htmlwidgets_1.6.2    
 [13] cachem_1.0.8          uuid_1.1-0            mime_0.12             lifecycle_1.0.3      
 [17] pkgconfig_2.0.3       Matrix_1.5-4.1        R6_2.5.1              fastmap_1.1.1        
 [21] tidycensus_1.4.1      shiny_1.7.4           digest_0.6.31         colorspace_2.1-0     
 [25] ps_1.7.5              pkgload_1.3.2         Hmisc_5.1-0           fansi_1.0.4          
 [29] timechange_0.2.0      httr_1.4.6            polyclip_1.10-4       abind_1.4-5          
 [33] compiler_4.3.1        proxy_0.4-27          splancs_2.01-43       remotes_2.4.2        
 [37] withr_2.5.0           htmlTable_2.4.1       backports_1.4.1       DBI_1.1.3            
 [41] RgoogleMaps_1.4.5.3   pkgbuild_1.4.1        R.utils_2.12.2        rappdirs_0.3.3       
 [45] sessioninfo_1.2.2     classInt_0.4-9        tools_4.3.1           units_0.8-2          
 [49] foreign_0.8-84        httpuv_1.6.11         nnet_7.3-19           R.oo_1.25.0          
 [53] glue_1.6.2            callr_3.7.3           promises_1.2.0.1      grid_4.3.1           
 [57] sf_1.0-13             checkmate_2.2.0       cluster_2.1.4         generics_0.1.3       
 [61] gtable_0.3.3          spatstat.data_3.0-1   tzdb_0.4.0            R.methodsS3_1.8.2    
 [65] class_7.3-22          tidyr_1.3.0           data.table_1.14.8     hms_1.1.3            
 [69] xml2_1.3.4            utf8_1.2.3            spatstat.geom_3.2-1   pillar_1.9.0         
 [73] later_1.3.1           lattice_0.21-8        tidyselect_1.2.0      miniUI_0.1.1.1       
 [77] knitr_1.43            gridExtra_2.3         xfun_0.39             stringi_1.7.12       
 [81] evaluate_0.21         interp_1.1-4          sgeostat_1.0-27       tibble_3.2.1         
 [85] cli_3.6.1             rpart_4.1.19          xtable_1.8-4          munsell_0.5.0        
 [89] processx_3.8.1        Rcpp_1.0.10           spatstat.random_3.1-5 WDI_2.7.8            
 [93] png_0.1-8             ellipsis_0.3.2        prettyunits_1.1.1     jpeg_0.1-10          
 [97] profvis_0.3.8         urlchecker_1.0.1      bitops_1.0-7          tigris_2.0.3         
[101] e1071_1.7-13          purrr_1.0.1           crayon_1.5.2          rlang_1.1.1          
[105] rvest_1.0.3          

Solution

  • Update: This bug was introduced by a change in the R language itself (v4.3.0). I have updated choroplethr to work around the issue. The version of choroplethr that has the fix is v3.7.2 and you can get it by typing install.packages("choroplethr") at the console. I am leaving my original answer here in case others want to better understand the underlying issue.


    I am the author of choroplethr. Thank you for reporting this bug. I can reproduce it locally, on the latest version of R (v4.3.1). I have tracked the issue down to this change which was introduce in R version 4.3.0:

    CHANGES IN R 4.3.0:

    SIGNIFICANT USER-VISIBLE CHANGES:

    • Calling && or || with LHS or (if evaluated) RHS of length greater than one is now always an error, with a report of the form

      'length = 4' in coercion to 'logical(1)'
      

      Environment variable R_CHECK_LENGTH_1_LOGIC2 no longer has any effect.

    (You can see the entire changelist here.)

    That change causes this line of code to fail:

          if (private$zoom == "alaska" || private$zoom == "hawaii") {
            choro = self$render_helper(self$choropleth.df, self$scale_name, self$theme_clean()) + ggtitle(self$title)
            if (self$add_state_outline)
            {
              choro + self$render_state_outline(private$zoom)
            }        
          } else {
            # remove AK and HI from the "real" df
            continental.df = self$choropleth.df[!self$choropleth.df$state %in% c("alaska", "hawaii"), ]
            continental.ggplot = self$render_helper(continental.df, self$scale_name, self$theme_clean()) + ggtitle(self$title)
            if (self$add_state_outline)
            {
              continental.regions = subset(private$zoom, private$zoom!="alaska" & private$zoom!="hawaii")
              continental.ggplot = continental.ggplot + self$render_state_outline(continental.regions)
            }
    

    In short, when you create a national map, the zoom parameter has length 51 (50 states plus DC), which is causing the error.

    My first reaction is that I have no idea why they introduced this change. It is not a hard error to work around. But I would like to spend some time better understanding why they made this change and if they have a preferred way to implement this logic before submitting a fix.