英文:
R-Shiny, Use action button into a leaflet popup inside a Shiny module
问题
我正在尝试在Shiny模块中的Leaflet弹出窗口中使用一个actionbutton
当尝试在Shiny模块的Leaflet弹出窗口中使用actionbutton时,按钮无法工作。
请参考下面的示例:
library(shiny)
library(leaflet)
library(DT)
map_ui <- function(id) {
ns <- NS(id)
tagList(
leafletOutput(ns("mymap"))
)
}
map_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
mapdata <- datasets::quakes
mapdata$latitude <- as.numeric(mapdata$lat)
mapdata$longitude <- as.numeric(mapdata$long)
mapdata$id <- 1:nrow(mapdata)
output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(lat = ~ latitude, lng = ~ longitude,
data = mapdata,
layerId = mapdata$id,
popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "Modal", onclick = 'Shiny.setInputValue("button_click", this.id, {priority: "event"})')))
})
observeEvent(input$button_click, {
showModal(modalDialog(
title = "TEST MODAL"
))
})
}
)
}
ui <- fluidPage(
map_ui('ex1')
)
server <- function(input, output){
map_Server('ex1')
}
shinyApp(ui, server)
有没有办法使该模块中的按钮工作?我认为问题可能是按钮没有使用ns(),但我找不到使其工作的方法。
谢谢。
英文:
I am trying to use an actionbutton in a leaflet popup into a shiny module
When trying to use an action button into a leaflet popup in a Shiny module, button do not work.
See the exemple below :
library(shiny)
library(leaflet)
library(DT)
map_ui <- function(id) {
ns <- NS(id)
tagList(
leafletOutput(ns("mymap"))
)
}
map_Server <- function(id) {
moduleServer(
id,
function(input, output, session) {
mapdata <- datasets::quakes
mapdata$latitude <- as.numeric(mapdata$lat)
mapdata$longitude <- as.numeric(mapdata$long)
mapdata$id <- 1:nrow(mapdata)
output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(lat = ~ latitude, lng = ~ longitude,
data = mapdata,
layerId = mapdata$id,
popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "Modal", onclick = 'Shiny.setInputValue(\"button_click\", this.id, {priority: \"event\"})')))
})
observeEvent(input$button_click, {
showModal(modalDialog(
title = "TEST MODAL"
)) })
}
)
}
ui <- fluidPage(
map_ui('ex1')
)
server <- function(input, output){
map_Server('ex1')
}
shinyApp(ui, server)
Is there any way to make work that button into the module ? I think that it comes that the button is not ns() but i don't find a way to make it works.
Thanks
答案1
得分: 1
是的,你需要添加ns:
function(input, output, session) {
ns <- session$ns
......
output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(
lat = ~ latitude, lng = ~ longitude,
data = mapdata,
layerId = mapdata$id,
popup =
~paste(
"<b>", mag, "</b></br>",
actionLink(
inputId = "modal", label = "Modal",
onclick = sprintf(
'Shiny.setInputValue("%s", this.id, {priority: "event"})',
ns("button_click")
)
)
)
)
})
......
}
英文:
Yes, you have to add the ns:
function(input, output, session) {
ns <- session$ns
......
output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(
lat = ~ latitude, lng = ~ longitude,
data = mapdata,
layerId = mapdata$id,
popup =
~paste(
"<b>", mag, "</b></br>",
actionLink(
inputId = "modal", label = "Modal",
onclick = sprintf(
'Shiny.setInputValue(\"%s\", this.id, {priority: \"event\"})',
ns("button_click")
)
)
)
)
})
......
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论