在R中自定义leaflet弹出窗口

6
我正在使用RStudio创建一个区域分布地图。我在导入到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>")

有什么想法可以实现它吗?
1个回答

17

概述

阅读完R, leaflet package, Passing a character vector of HTML tags to popups?后,以下是您可以修改现有代码的方法:

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

可重现的示例

我使用世界边界数据集下载每个国家的形状文件。然后为数据集中的每个国家添加维基百科网址。

带弹出窗口到维基百科的Leaflet地图截图

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

标签选项样式列表需要格式化为 'code' labelOptions(style = list("font-weight" = "normal", "padding" = "3px 8px", "font-size" = "42px", "direction" = "auto" ))。 - kraggle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接