gpt4 book ai didi

r - 从 R 中的传单包创建 html 时字符编码错误

转载 作者:行者123 更新时间:2023-12-01 05:02:44 24 4
gpt4 key购买 nike

当我在 R 中使用传单包(不是leafletR)创建 map 时,它不会转换我的丹麦语字符。他们似乎都被打乱了。我找不到解决方案。该问题会影响图例和弹出窗口。

代码如下所示:

colorscale = colorNumeric("YlOrRd", domain = NULL)

data_popup <- paste0("Med en gennemsnitsalder på <strong>", round(kommuner$`NUVÆRENDE ALDER`,2), "</strong> år ligner ",
"<strong>", kommuner$name, "</strong> mest Danmark i år ",
"<strong>", kommuner$ÅRSTAL,
ifelse(kommuner$ÅRSTAL > 2015, "</strong>, hvor gennemsnitsalderen vil være ", "</strong>, hvor gennemsnitsalderen var "),
"<strong>", round(kommuner$`ÅRSTAL ALDER`,2), "</strong> år")

kort <- leaflet(kommuner) %>% addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = ~colorscale(ÅRSTAL),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = data_popup) %>%
addLegend("bottomright", pal = colorscale, values = ~ÅRSTAL,
title = "Årstal",
labFormat = labelFormat(prefix = "År ", big.mark = ""),
opacity = 1) %>%
setView(10.533880, 56.114586, zoom=8)

kort

我该如何解决这个问题?

更新:可重现的例子
library(sp)
Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr4, Sr3), "s3/4")
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)

df <- data.frame(a = c("A", "B", "C"), b = c("Æ", "Ø", "Å"))

data_popup <- paste0(df$a, " is no problem.",
"<br>",df$b, " is a problem.")

library(leaflet)
leaflet(height = "300px") %>% addPolygons(data = SpP, popup = data_popup)

单击空间多边形时,我无法读取“Æ、Ø 和 Å”

更新: session 信息
Session info -------------------------------------------------------------------
setting value
version R version 3.2.1 (2015-06-18)
system x86_64, mingw32
ui RStudio (0.99.467)
language (EN)
collate Danish_Denmark.1252
tz Europe/Paris

Packages -----------------------------------------------------------------------
package * version date source
assertthat 0.1 2013-12-06 CRAN (R 3.2.0)
colorspace 1.2-6 2015-03-11 CRAN (R 3.2.0)
curl 0.9 2015-06-19 CRAN (R 3.2.1)
DBI 0.3.1 2014-09-24 CRAN (R 3.2.0)
devtools 1.8.0 2015-05-09 CRAN (R 3.2.0)
digest 0.6.8 2014-12-31 CRAN (R 3.2.0)
dplyr * 0.4.2 2015-06-16 CRAN (R 3.2.1)
git2r 0.10.1 2015-05-07 CRAN (R 3.2.0)
htmltools 0.2.6 2014-09-08 CRAN (R 3.2.0)
htmlwidgets 0.5 2015-06-21 CRAN (R 3.2.1)
jsonlite 0.9.16 2015-04-11 CRAN (R 3.2.0)
lattice 0.20-31 2015-03-30 CRAN (R 3.2.1)
leaflet * 1.0.0 2015-06-24 CRAN (R 3.2.1)
magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
memoise 0.2.1 2014-04-22 CRAN (R 3.2.0)
munsell 0.4.2 2013-07-11 CRAN (R 3.2.0)
plyr 1.8.3 2015-06-12 CRAN (R 3.2.1)
R6 2.0.1 2014-10-29 CRAN (R 3.2.0)
RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.2.0)
Rcpp 0.11.6 2015-05-01 CRAN (R 3.2.0)
rgdal * 1.0-4 2015-06-23 CRAN (R 3.2.1)
rstudioapi 0.3.1 2015-04-07 CRAN (R 3.2.0)
rversions 1.0.1 2015-06-06 CRAN (R 3.2.1)
scales 0.2.5 2015-06-12 CRAN (R 3.2.1)
sp * 1.1-1 2015-06-05 CRAN (R 3.2.0)
stringi 0.5-4 2015-06-28 CRAN (R 3.2.1)
stringr * 1.0.0 2015-04-30 CRAN (R 3.2.0)
xml2 0.1.1 2015-06-02 CRAN (R 3.2.1)
yaml 2.1.13 2014-06-12 CRAN (R 3.2.0)

最佳答案

我认为,整体解决方案是使用 UTF-8 字符编码(参见: https://en.wikipedia.org/wiki/UTF-8 )作为您计算机系统的基础。简而言之,放弃使用“Danish_Denmark.[ISO]1252” - 或者就此而言“Slovenian_Slovenia.[ISO]1250”。这么说是因为我认为大多数语言特定的字母应该已经在 UTF-8 中可用。

作为传单图尝试的临时解决方案,在 RStudio 中:工具 > 项目选项 ... > 代码编辑 > 文本编码 > 更改 ....(到 UTF-8)。

希望这有效,

埃纳尔

关于r - 从 R 中的传单包创建 html 时字符编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com