I am trying to create a legend with multiple lines as shown in this leaflet example, but using the mapview package instead.
Following the above example, these cases work fine:
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"
)
However, introducing the </br> or <br> into the mapview layer.name introduces errors.
p %>%
mapview(
zcol = 'AREA',
stroke = FALSE,
alpha = 1,
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',
stroke = FALSE,
alpha = 1,
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() below
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
Any advice on how to solve this?
It looks like this was an issue related to the caching of map files in the .Rproj.user and R temporary directory folders; I deleted the contents of the .Rproj.user shared/notebooks subfolder and cleared out the R temporary directory at tempdir(), after a restart of RStudio and R, the map legends were created correctly. Thank you for @iago for confirming in the comment that the sample code worked on Linux.