问题描述
我正在尝试创建一个多行的图例,如 this leaflet example 所示,但使用的是 mapview 包。
按照上面的例子,这些情况可以正常工作:
library(leaflet)
library(raster)
library(mapview)
p <- shapefile(system.file("external/lux.shp",package="raster"))
pal <- colorNumeric(
palette = "Reds",domain = p$AREA)
p %>%
leaflet() %>%
addTiles() %>%
addpolygons(stroke=FALSE,color = ~pal(AREA),fillOpacity = 1) %>%
addLegend(pal = pal,values = ~AREA,title = "line 1 </br> line 2")
p %>%
mapview(
zcol = 'AREA',stroke = FALSE,alpha = 1,layer.name = "line 1"
)
但是,将 或
引入 mapview layer.name 会引入错误。
p %>%
mapview(
zcol = 'AREA',layer.name = "line 1 </br> line 2"
)
#Error in validateScalarName(name) :
# Invalid argument 'name' (must be a non-empty character string and contain no '/' or '\')
#In addition: Warning message:
#In file.create(to[okay]) :
# cannot create file 'C:/RTMP\Rtmp6rsB0P\file2a687e904718/line1</br>line2_layer.fgb',reason #'Invalid argument'
p %>%
mapview(
zcol = 'AREA',layer.name = "line 1 <br> line 2"
)
#Error in normalizePath(path.expand(path),winslash,mustWork) :
# path[1]="lib/line1<br>line2-0.0.1": The filename,directory name,or volume label Syntax is #incorrect
#In addition: Warning messages:
#1: In file.create(to[okay]) :
# cannot create file 'C:/RTMP\Rtmp6rsB0P\file2a6843ec14e6/line1<br>line2_layer.fgb',reason #'Invalid argument'
#2: In dir.create(target_dir) :
# cannot create dir 'lib\line1<br>line2-0.0.1',reason 'Invalid argument'
sessionInfo() 下面
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics Grdevices datasets utils
[6] methods base
other attached packages:
[1] mapview_2.10.0 raster_3.1-5 sp_1.4-5
[4] leaflet_2.0.4.1 shiny_1.6.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 lattice_0.20-41
[3] leaflet.providers_1.9.0 png_0.1-7
[5] class_7.3-17 ps_1.6.0
[7] assertthat_0.2.1 digest_0.6.25
[9] utf8_1.2.1 mime_0.9
[11] R6_2.5.0 reprex_2.0.0
[13] stats4_4.0.3 evaluate_0.14
[15] e1071_1.7-3 highr_0.8
[17] pillar_1.6.0 rlang_0.4.10
[19] rstudioapi_0.13 miniui_0.1.1.1
[21] callr_3.7.0 jquerylib_0.1.4
[23] rmarkdown_2.9 rgdal_1.5-23
[25] webshot_0.5.2 htmlwidgets_1.5.1
[27] munsell_0.5.0 compiler_4.0.3
[29] httpuv_1.6.1 xfun_0.24
[31] pkgconfig_2.0.3 base64enc_0.1-3
[33] clipr_0.7.0 htmltools_0.5.1.1
[35] tidyselect_1.1.0 tibble_3.1.1
[37] codetools_0.2-16 fansi_0.4.2
[39] dplyr_1.0.5 Crayon_1.4.1
[41] withr_2.4.2 later_1.0.0
[43] sf_0.9-8 grid_4.0.3
[45] jsonlite_1.6.1 satellite_1.0.2
[47] xtable_1.8-4 lifecycle_1.0.0
[49] DBI_1.1.1 magrittr_2.0.1
[51] units_0.6-6 scales_1.1.0
[53] KernSmooth_2.23-17 cli_2.4.0
[55] cachem_1.0.5 farver_2.0.3
[57] renv_0.13.2 fs_1.5.0
[59] promises_1.1.0 bslib_0.2.5.1
[61] generics_0.0.2 ellipsis_0.3.1
[63] vctrs_0.3.7 RColorBrewer_1.1-2
[65] tools_4.0.3 leafem_0.1.3
[67] glue_1.4.2 purrr_0.3.4
[69] crosstalk_1.1.0.1 processx_3.5.2
[71] fastmap_1.1.0 yaml_2.2.1
[73] colorspace_1.4-1 classInt_0.4-3
[75] knitr_1.28 sass_0.4.0
关于如何解决这个问题有什么建议吗?
解决方法
看起来这是一个与缓存 .Rproj.user 和 R 临时目录文件夹中的地图文件有关的问题;我删除了 .Rproj.user shared/notebooks 子文件夹的内容并清除了 tempdir() 中的 R 临时目录,在重新启动 RStudio 和 R 后,正确创建了地图图例。感谢 @iago 在评论中确认示例代码可在 Linux 上运行。