問題描述
我正在使用 RStudio 創(chuàng)建一個等值線傳單地圖.我在導(dǎo)入到 R 的 shapefile 中有 Country 和 Url 作為屬性.
我希望在最終地圖的彈出窗口中將國家名稱和 URL 顯示為超鏈接.
以下是我目前使用的代碼:
m <- world_shapefiles %>%傳單() %>%addProviderTiles(providers$Esri.WorldStreetMap) %>%添加多邊形(標(biāo)簽=?國家,labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px", textsize = "15px",方向=自動")),popup = ~ paste("國家:", 國家, "<br/>","<b/>","URL:", url))
我想在彈出窗口中看到文本單擊此處"而不是整個 url,我嘗試使用以下代碼但沒有成功.
popup = ~ paste("Country:", counry, "<br/>","<b/>","URL:", "<b><a href=url>點(diǎn)擊這里</a></b>")
有什么想法可以實(shí)現(xiàn)嗎?
概覽
看完
# 加載必要的包圖書館(傳單)圖書館(SF)# 下載壓縮文件下載文件(url = "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip", destfile = "TM_WORLD_BORDERS-0.3.zip")# 解壓解壓縮(zipfile =TM_WORLD_BORDERS-0.3.zip")# 轉(zhuǎn)換為 sf世界.邊界 <-read_sf(dsn = getwd(),層=TM_WORLD_BORDERS-0.3")# 添加每個國家的維基百科頁面world.borders$wiki <-paste0("https://en.wikipedia.org/wiki/", world.borders$NAME)# 制作傳單地圖我的.map <-傳單(選項(xiàng)=傳單選項(xiàng)(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(數(shù)據(jù) = world.borders, 填充 = "#D24618", 顏色 = "#D24618", 不透明度 = 0.5, 填充不透明度 = 0.01, 重量 = 3, 彈出 = paste0(<b>國家:</b>", world.borders$NAME, "<br>", "
I am using RStudio to create a choropleth leaflet map. I have Country and Url as an attribute in the shapefile that I imported to R.
I wish to show the Country name and URL as a hyperlink within the popup of the final map.
Below is the code I have used so far:
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)
)
I want to see the text "Click here" instead of the entire url in the popup, I tried using the below code with no luck.
popup = ~ paste("Country:", counry, "<br/>","<b/>","URL:", "<b><a href=url>Click Here</a></b>")
Any ideas to achieve it?
Overview
After reading R, leaflet package, Passing a character vector of HTML tags to popups?, here's how you would modify your existing code:
# 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>"
)
Reproducible Example
I use the World Borders Data Set to download shapefiles for each country in the world. I then add a Wikipedia URL for each country in the data set.
# 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 #
這篇關(guān)于在 R 中自定義傳單彈出窗口的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!