gpt4 book ai didi

R Shiny 带传单 : create a modal window on clicking an icon

转载 作者:行者123 更新时间:2023-12-02 02:38:36 24 4
gpt4 key购买 nike

当我单击 Shiny 的传单 map 中的图标时,我想创建一个模式窗口。这可行吗?我尝试了下面的代码,但是 bsModal 没有执行任何操作。

library(shiny)
library(leaflet)
library(shinyBS)

points <- data.frame(cbind(latitude = rnorm(40) * 2 + 13, longitude =
rnorm(40) + 48))


ui <- fluidPage(
leafletOutput("mymap"),
bsModal("modalExample", "This will open a modal", "assign_task", size =
"large",
HTML(""))
)

server <- function(input, output, session) {



output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(lat = ~ latitude, lng = ~ longitude,
data = points,
popup=~ sprintf(
'<button type="button" id="assign_task">Open Modal </button>'
))
})
}

shinyApp(ui, server)

enter image description here

最佳答案

我将发布两种可能的解决方案。第一个是我认为最适合您需求的解决方案,第二个更适合您当前的代码。希望这有帮助!

<小时/>

解决方案1:

library(shiny)
library(leaflet)

points <- data.frame(cbind(id=seq(1,40),latitude = rnorm(40) * 2 + 13, longitude =
rnorm(40) + 48))


ui <- fluidPage(
leafletOutput("mymap"),
actionButton("action1","Show modal")
)

server <- function(input, output, session) {

observeEvent(input$mymap_marker_click, {
id = input$mymap_marker_click$id
showModal(modalDialog(
title = "You selected a marker!",
paste0("ID: ", id, ", lat: ", round(points$latitude[id==id],2),", lon: ", round(points$longitude[id==id],2))
))
})

output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(layerId = ~ id,lat = ~ latitude, lng = ~ longitude,
data = points
)
})
}

shinyApp(ui, server)
<小时/>

解决方案2:

library(shiny)
library(leaflet)
library(shinyBS)

points <- data.frame(cbind(latitude = rnorm(40) * 2 + 13, longitude =
rnorm(40) + 48))


ui <- fluidPage(
leafletOutput("mymap"),
actionButton("action1","Show modal")
)

server <- function(input, output, session) {

observeEvent(input$button_click, {
showModal(modalDialog(
title = "Important message",
"This is an important message!"
))
})

output$mymap <- renderLeaflet({
leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
addMarkers(lat = ~ latitude, lng = ~ longitude,
data = points,
popup= ~paste("<b>", latitude, longitude, "</b></br>", actionButton("showmodal", "Show modal", onclick = 'Shiny.onInputChange(\"button_click\", Math.random())')))
})
}

shinyApp(ui, server)

关于R Shiny 带传单 : create a modal window on clicking an icon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com