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

时间:2015-07-22 10:25:47

标签: r leaflet

当我在R中使用传单包(而不是leafletR)创建地图时,它不会转换我的丹麦字符。他们好像都搞砸了。我找不到解决方案。该问题会影响图例和弹出窗口。

代码如下所示:

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 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)

1 个答案:

答案 0 :(得分:1)

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

作为小叶图试验的临时解决方案,在RStudio中:工具&gt;项目选项...&gt;代码编辑&gt;文字编码&gt;改变....(改为UTF-8)。

希望这有效,

埃纳尔