Skip to content

Instantly share code, notes, and snippets.

@yamano357
Last active November 11, 2015 11:49
Show Gist options
  • Save yamano357/ce74577c6b675a350351 to your computer and use it in GitHub Desktop.
Save yamano357/ce74577c6b675a350351 to your computer and use it in GitHub Desktop.
# Windows環境で{leaflet}のpopup引数を指定すると表示ができない症状について
library(dplyr)
library(readr)
library(stringr)
library(leaflet)
plotLeaflet <- function (plot_obj){
return(
plot_obj %>%
leaflet::leaflet() %>%
leaflet::addCircles(
lng = ~longitude, lat = ~latitude,
stroke = FALSE,
popup = ~ htmltools::htmlEscape(text = station)
) %>%
leaflet::addTiles()
)
}
download.file(url = "https://gist.githubusercontent.com/yamano357/929eec744d72ee88fa0b/raw/1bb01fe844f2ae1d6eef8664cfb922d61843aff4/station.csv", destfile = "station.csv")
test_ok <- dplyr::data_frame(station = "伊予西条", longitude = 133.1876, latitude = 33.91246) %>%
print
# Source: local data frame [1 x 3]
# station longitude latitude
# (chr) (dbl) (dbl)
# 1 伊予西条 133.1876 33.91246
test_ok2 <- readr::read_delim(file = "station.csv", col_names = TRUE, delim = ",") %>%
print
# Source: local data frame [1 x 3]
# station longitude latitude
# (chr) (dbl) (dbl)
# 1 莨贋コ郁・ソ譚。 133.1876 33.91246
test_ng <- test_ok2 %>%
dplyr::mutate(station = stringr::str_conv(string = .$station, encoding = "UTF-8")) %>%
print
# Source: local data frame [1 x 3]
# station longitude latitude
# (chr) (dbl) (dbl)
# 1 伊予西条 133.1876 33.91246
# Linux上だと全部同じ
charToRaw(x = test_ok$station)
# [1] 88 c9 97 5c 90 bc 8f f0
charToRaw(x = test_ok2$station)
# [1] e4 bc 8a e4 ba 88 e8 a5 bf e6 9d a1
charToRaw(x = test_ng$station)
# [1] e4 bc 8a e4 ba 88 e8 a5 bf e6 9d a1
# Linux上だとtest_okも"UTF-8"
Encoding(x = test_ok$station)
# [1] "unknown"
Encoding(x = test_ok2$station)
# [1] "unknown"
Encoding(x = test_ng$station)
# [1] "UTF-8"
# Linux上だと全部OK
plotLeaflet(plot_obj = test_ok) # OK(ただし、ポップアップ文字化け)
plotLeaflet(plot_obj = test_ok2) # OK
plotLeaflet(plot_obj = test_ng) # NG(RStudio上のViewerタブが灰色)
Encoding(x = test_ng$station) <- "unknown"
plotLeaflet(plot_obj = test_ng)# OKに
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment