如何使点击 R Leaflet 状态转到 url?
How to make a click on R Leaflet state go to a url?
我想构建一个地图,当点击某个州时,它会在新选项卡中打开一个超链接 url。我一直在四处寻找答案,但无法完全将其用于 r 传单。我认为我需要编辑 onRender
中的 json,但我似乎没有做对。
非常感谢!!
library(tidyverse)
library(tigris)
library(leaflet)
state <-
states(
cb = TRUE
) %>%
filter(STUSPS %in% c('CA', 'WA', 'MD')) %>%
mutate(win_url =
case_when(
STUSPS == 'CA' ~ str_c('<a href=https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/>California Evictions</a>'),
STUSPS == 'WA' ~ str_c('<a href=https://evictions.study/washington/maps/summary.html>Washington Evictions</a>'),
STUSPS == 'MD' ~ str_c('<a href=https://evictions.study/maryland/report/baltimore.html>Maryland Evictions</a>')))
leaflet(state) %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>% # higher zIndex rendered on top
htmlwidgets::onRender('function onclick(e) {
window.open(e.target.feature.properties.win_url);
}') %>%
addProviderTiles("CartoDB.PositronNoLabels") %>%
addProviderTiles("CartoDB.PositronOnlyLabels",
options = leafletOptions(pane = "maplabels"),
group = "map labels") %>%
addPolygons(
# label = ~'<a href="https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/">California Evictions</a>', # this is an option but not preferred
fillOpacity = .5,
color = ~'Red',
stroke = TRUE,
weight = 1,
opacity = .5,
highlightOptions = highlightOptions(
color = "#ff4a4a",
weight = 5,
bringToFront = TRUE
)
)
使用 and looping through, from addEventListener using for loop and passing values 的答案似乎有效:
jsCode <- paste0('
function(el, x, data) {
var marker = document.getElementsByClassName("leaflet-interactive");
for(var i=0; i < marker.length; i++){
(function(){
var v = data.win_url[i];
marker[i].addEventListener("click", function() { window.open(v);}, false);
}());
}
}
')
和运行
leaflet(state) %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>%
addProviderTiles("CartoDB.PositronNoLabels") %>%
addProviderTiles("CartoDB.PositronOnlyLabels",
options = leafletOptions(pane = "maplabels"),
group = "map labels") %>%
addPolygons(
fillOpacity = .5,
color = 'Red',
stroke = TRUE,
weight = 1,
opacity = .5,
highlightOptions = highlightOptions(
color = "#ff4a4a",
weight = 5,
bringToFront = TRUE
)
) %>%
htmlwidgets::onRender(jsCode, data=state)
我不得不重新调整状态 URL
state <-
states(
cb = TRUE
) %>%
filter(STUSPS %in% c('CA', 'WA', 'MD')) %>%
mutate(win_url =
case_when(
STUSPS == 'CA' ~ 'https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/',
STUSPS == 'WA' ~ 'https://evictions.study/washington/maps/summary.html',
STUSPS == 'MD' ~ 'https://evictions.study/maryland/report/baltimore.html'))
我想构建一个地图,当点击某个州时,它会在新选项卡中打开一个超链接 url。我一直在四处寻找答案,但无法完全将其用于 r 传单。我认为我需要编辑 onRender
中的 json,但我似乎没有做对。
非常感谢!!
library(tidyverse)
library(tigris)
library(leaflet)
state <-
states(
cb = TRUE
) %>%
filter(STUSPS %in% c('CA', 'WA', 'MD')) %>%
mutate(win_url =
case_when(
STUSPS == 'CA' ~ str_c('<a href=https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/>California Evictions</a>'),
STUSPS == 'WA' ~ str_c('<a href=https://evictions.study/washington/maps/summary.html>Washington Evictions</a>'),
STUSPS == 'MD' ~ str_c('<a href=https://evictions.study/maryland/report/baltimore.html>Maryland Evictions</a>')))
leaflet(state) %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>% # higher zIndex rendered on top
htmlwidgets::onRender('function onclick(e) {
window.open(e.target.feature.properties.win_url);
}') %>%
addProviderTiles("CartoDB.PositronNoLabels") %>%
addProviderTiles("CartoDB.PositronOnlyLabels",
options = leafletOptions(pane = "maplabels"),
group = "map labels") %>%
addPolygons(
# label = ~'<a href="https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/">California Evictions</a>', # this is an option but not preferred
fillOpacity = .5,
color = ~'Red',
stroke = TRUE,
weight = 1,
opacity = .5,
highlightOptions = highlightOptions(
color = "#ff4a4a",
weight = 5,
bringToFront = TRUE
)
)
使用
jsCode <- paste0('
function(el, x, data) {
var marker = document.getElementsByClassName("leaflet-interactive");
for(var i=0; i < marker.length; i++){
(function(){
var v = data.win_url[i];
marker[i].addEventListener("click", function() { window.open(v);}, false);
}());
}
}
')
和运行
leaflet(state) %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>%
addProviderTiles("CartoDB.PositronNoLabels") %>%
addProviderTiles("CartoDB.PositronOnlyLabels",
options = leafletOptions(pane = "maplabels"),
group = "map labels") %>%
addPolygons(
fillOpacity = .5,
color = 'Red',
stroke = TRUE,
weight = 1,
opacity = .5,
highlightOptions = highlightOptions(
color = "#ff4a4a",
weight = 5,
bringToFront = TRUE
)
) %>%
htmlwidgets::onRender(jsCode, data=state)
我不得不重新调整状态 URL
state <-
states(
cb = TRUE
) %>%
filter(STUSPS %in% c('CA', 'WA', 'MD')) %>%
mutate(win_url =
case_when(
STUSPS == 'CA' ~ 'https://shiny.demog.berkeley.edu/alexramiller/kqed-evictions/',
STUSPS == 'WA' ~ 'https://evictions.study/washington/maps/summary.html',
STUSPS == 'MD' ~ 'https://evictions.study/maryland/report/baltimore.html'))