英文:
state_choropleth not working for >1 state
问题
尝试一个非常基本的州级分布地图:
library(choroplethr)
library(choroplethrMaps)
state_choropleth(df_pop_state)
出现以下错误:
Error in private$zoom == "alaska" || private$zoom == "hawaii" :
'length = 51' 在强制转换为 'logical(1)' 时出错
这个错误与这里找到的错误相同,尽管在那种情况下,地图似乎已经生成。
如果我缩放到一个州,它可以正常工作:
state_choropleth(df_pop_state, zoom=c("washington"))
不知道出了什么问题。我相当确定一切都是最新的。几个月前我进行了R升级(也许是从版本3到4?),所以也许与此有关。
sessionInfo()
的结果如下:
R 版本 4.3.1 (2023-06-16)
平台:aarch64-apple-darwin20 (64 位)
运行在: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 版本 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
时区:Europe/Rome
tzcode 源: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
...
以上是您提供的代码和信息的翻译。
英文:
Trying a very basic state choropleth:
<!-- language: R -->
library(choroplethr)
library(choroplethrMaps)
state_choropleth(df_pop_state)
It exits with this error:
<!-- language: R -->
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:
<!-- language: R -->
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
答案1
得分: 1
更新:此错误是由 R 语言本身的更改引入的(版本为 4.3.0)。我已经更新了 choroplethr 以解决这个问题。已经修复此问题的 choroplethr 版本是 v3.7.2,您可以在控制台输入 install.packages("choroplethr")
来获取它。我将保留我的原始答案,以便其他人想更好地理解底层问题。
我是 choroplethr 的作者。感谢您报告此错误。我可以在本地重现它,使用最新版本的 R(v4.3.1)。我已经追踪到这个问题是由于 R 版本 4.3.0 中引入的这个更改引起的:
> R 4.3.0 中的更改:
>
> 重要的用户可见更改:
>
> * 使用左操作数(LHS)或(如果已评估)右操作数(RHS)的长度大于一现在总是会报错,报错信息的形式如下:
>
> 'length = 4' in coercion to 'logical(1)'
>
> 环境变量 R_CHECK_LENGTH_1_LOGIC2 不再起作用。
>
(您可以在此处看到整个更改列表。)
这个更改导致这个代码行失败:
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)
}
简而言之,当您创建国家地图时,zoom
参数的长度为 51(50 个州加华盛顿特区),这导致了错误。
我的第一反应是我不知道他们为什么要引入这个更改。这不是一个难以解决的错误。但在提交修复之前,我希望花一些时间更好地理解他们为什么要做出这个更改,以及他们是否有一种首选的实现此逻辑的方式。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论