Wrong character encoding when generating html from leaflet package in R

When I create a map using leaflet package (not leafletR) in R, it doesn't convert my Danish characters. They all seem to be climbing. I cannot find a solution. The issue affects both the legend and the popup.

The code looks like this:

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 

      

How to solve this problem?

UPDATE: Reproducible example

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)

      

I cannot read "Æ, Ø and Å" when I click on 3D polygons

UPDATE: Session Information

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)

      

+3


source to share


1 answer


The general solution, I think, is to use the UTF-8 character encoding (see https://en.wikipedia.org/wiki/UTF-8 ) as a base on your system machine. briefly stop using "Danish_Denmark. [ISO] 1252" - or, for that matter, "Slovenian_Slovenia. [ISO] 1250". I will say this because I think most letter-specific languages ​​should already be available in UTF-8.

as an intermediate solution for trial firmware in RStudio: Tools> Project Options ...> Code Editing> Text Encoding> Change ... (to UTF-8).



hope it works,

Einar

+1


source







All Articles