2017-12-13 30 views
1

저는 RStudio를 사용하여 choropleth 전단지를 작성하고 있습니다. R에 가져온 shapefile의 속성으로 국가 및 URL이 있습니다.리플렛 팝업 사용자 정의 R

최종지도 팝업에서 국가 이름과 URL을 하이퍼 링크로 표시하고 싶습니다. 다음은

내가 지금까지 사용했던 코드 :

m <- world_shapefiles %>% 
    leaflet() %>% 
    addProviderTiles(providers$Esri.WorldStreetMap) %>%  
    addPolygons( 
     label=~country, 
      labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px", textsize = "15px", 
direction = "auto")), 
       popup = ~ paste("Country:", country, "<br/>","<b/>","URL:", url) 
) 

내가 텍스트를 볼 "여기를 클릭"할 대신 팝업에서 전체 URL, 나는 행운과 함께 아래의 코드를 사용하여 시도했다.

popup = ~ paste("Country:", counry, "<br/>","<b/>","URL:", "<b><a href=url>Click Here</a></b>") 

어떤 아이디어가 있습니까? 나는 세계 각 국가 shape 파일을 다운로드 할 World Borders Data Set를 사용

# it seems ~ doesn't work inside of the paste0() function 
# which is why I accessed the variables through the $ 
popup = paste0("Country:" 
       , world_shapefiles$country 
       , "<br>" 
       , "<a href='" 
       , world_shapefiles$url 
       , "' target='_blank'>" 
       , "Click Here</a>" 
       ) 

재현 예

:

답변

0

개요

R, leaflet package, Passing a character vector of HTML tags to popups?을 읽은 후, 여기에 기존 코드를 수정 할 방법입니다. 그런 다음 데이터 세트의 각 국가에 Wikipedia 개의 URL을 추가합니다.

SS of Leaflet Map w/popup to Wiki

# load necessary packages 
library(leaflet) 
library(sf) 

# download zip file 
download.file(
    url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip" 
    , destfile = "TM_WORLD_BORDERS-0.3.zip" 
) 

# unzip 
unzip(zipfile = "TM_WORLD_BORDERS-0.3.zip") 

# transfrom to sf 
world.borders <- 
    read_sf(dsn = getwd() 
      , layer = "TM_WORLD_BORDERS-0.3") 

# add the wikipedia page for each country 
world.borders$wiki <- 
    paste0("https://en.wikipedia.org/wiki/", world.borders$NAME) 

# make leaflet map 
my.map <- 
    leaflet(options = leafletOptions(minZoom = 2)) %>% 
    setMaxBounds(lng1 = -180 
       , lat1 = -89.98155760646617 
       , lng2 = 180 
       , lat2 = 89.99346179538875) %>% 
    addTiles(urlTemplate = "https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}") %>% 
    addPolygons(data = world.borders 
       , fill = "#D24618" 
       , color = "#D24618" 
       , opacity = 0.5 
       , fillOpacity = 0.01 
       , weight = 3 
       , popup = paste0(
       "<b>Country: </b>" 
       , world.borders$NAME 
       , "<br>" 
       , "<a href='" 
       , world.borders$wiki 
       , "' target='_blank'>" 
       , "Click Here to View Wiki</a>" 
       ) 
       , label = ~NAME 
       , labelOptions = labelOptions(
       style = list("font-weight" = "normal" 
           , padding = "3px 8px" 
           , textsize = "15px" 
           , direction = "auto")) 
       , highlightOptions = highlightOptions( 
       color = "#10539A" 
       , weight = 3 
       , fillColor = NA 
       )) 

# display map 
my.map 

# end of script #