在Windows机器上,我遇到了编码问题。我正在使用leaflet
和rmarkdown
制作交互式地图。关键是,地图标题和弹出窗口应使用俄语。当我刚创建leaflet
对象时,一切正常:
library(dplyr)
library(sp)
library(leaflet)
Sys.setlocale("LC_ALL", "Russian_Russia")
df <- tibble(X = rnorm(100, 28, 2),
y = rnorm(100, 42, 4),
id = seq(1, 100, 1),
text = c(rep("Валар", 50),
rep("Моргулис", 50))) %>%
SpatialPointsDataFrame(coords = .[, c(1:2)],
data = .,
proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
popup1 <- paste0("<span style='color: salmon;'><strong>РУССКИЙ ТЕКСТ: </strong></span>",
df$text)
leaflet() %>%
addProviderTiles("Esri.WorldStreetMap",
options = tileOptions(minZoom=1, maxZoom=14)) %>%
leaflet::addMarkers(data = df,
popup = popup1)
但是,当我尝试使用html
将此代码编织到rmarkdown
时,我得到一个普通的西里尔字母,并显示空弹出消息:
---
title: "Сэмпл текст"
output:
flexdashboard::flex_dashboard
---
```{r setup, echo=FALSE}
library(dplyr)
library(sp)
library(leaflet)
library(flexdashboard)
Sys.setlocale("LC_ALL", "Russian_Russia")
df <- tibble(X = rnorm(100, 28, 2),
y = rnorm(100, 42, 4),
id = seq(1, 100, 1),
text = c(rep("Валар", 50),
rep("Моргулис", 50))) %>%
SpatialPointsDataFrame(coords = .[, c(1:2)],
data = .,
proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
popup1 <- paste0("<span style='color: salmon;'><strong>РУССКИЙ ТЕКСТ: </strong></span>",
df$text)
leaflet() %>%
addProviderTiles("Esri.WorldStreetMap",
options = tileOptions(minZoom=1, maxZoom=14)) %>%
leaflet::addMarkers(data = df,
popup = popup1)
```
当我尝试在块选项{r setup, echo=FALSE, R.options=options(encoding = "utf-8")}
中指定编码时,一切都会变得更糟:
我的sessionInfo()
:
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=Russian_Russia.1251 LC_CTYPE=Russian_Russia.1251
[3] LC_MONETARY=Russian_Russia.1251 LC_NUMERIC=C
[5] LC_TIME=Russian_Russia.1251
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] sp_1.3-1 leaflet_2.0.2 sf_0.7-3 dplyr_0.8.0.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 pillar_1.3.1 compiler_3.5.1
[4] later_0.7.5 class_7.3-14 tools_3.5.1
[7] digest_0.6.18 evaluate_0.13 jsonlite_1.5
[10] tibble_2.0.1 lattice_0.20-35 pkgconfig_2.0.2
[13] rlang_0.3.1 shiny_1.2.0 DBI_1.0.0
[16] rstudioapi_0.8 crosstalk_1.0.0 yaml_2.2.0
[19] rgdal_1.3-9 xfun_0.6 e1071_1.7-0
[22] knitr_1.22 htmlwidgets_1.4 flexdashboard_0.5.1.1
[25] classInt_0.3-1 grid_3.5.1 tidyselect_0.2.5
[28] glue_1.3.0 R6_2.3.0 rmarkdown_1.12
[31] purrr_0.3.1 magrittr_1.5 promises_1.0.1
[34] htmltools_0.3.6 units_0.6-2 rsconnect_0.8.10
[37] assertthat_0.2.0 mime_0.6 xtable_1.8-3
[40] httpuv_1.4.5 crayon_1.3.4