R-Shiny,在Shiny模块内将动作按钮用于Leaflet弹出窗口。

huangapple go评论145阅读模式
英文:

R-Shiny, Use action button into a leaflet popup inside a Shiny module

问题

我正在尝试在Shiny模块中的Leaflet弹出窗口中使用一个actionbutton

当尝试在Shiny模块的Leaflet弹出窗口中使用actionbutton时,按钮无法工作。

请参考下面的示例:

  1. library(shiny)
  2. library(leaflet)
  3. library(DT)
  4. map_ui <- function(id) {
  5. ns <- NS(id)
  6. tagList(
  7. leafletOutput(ns("mymap"))
  8. )
  9. }
  10. map_Server <- function(id) {
  11. moduleServer(
  12. id,
  13. function(input, output, session) {
  14. mapdata <- datasets::quakes
  15. mapdata$latitude <- as.numeric(mapdata$lat)
  16. mapdata$longitude <- as.numeric(mapdata$long)
  17. mapdata$id <- 1:nrow(mapdata)
  18. output$mymap <- renderLeaflet({
  19. leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
  20. addMarkers(lat = ~ latitude, lng = ~ longitude,
  21. data = mapdata,
  22. layerId = mapdata$id,
  23. popup= ~paste("<b>", mag, "</b></br>", actionLink(inputId = "modal", label = "Modal", onclick = 'Shiny.setInputValue("button_click", this.id, {priority: "event"})')))
  24. })
  25. observeEvent(input$button_click, {
  26. showModal(modalDialog(
  27. title = "TEST MODAL"
  28. ))
  29. })
  30. }
  31. )
  32. }
  33. ui <- fluidPage(
  34. map_ui('ex1')
  35. )
  36. server <- function(input, output){
  37. map_Server('ex1')
  38. }
  39. 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 :

  1. library(shiny)
  2. library(leaflet)
  3. library(DT)
  4. map_ui &lt;- function(id) {
  5. ns &lt;- NS(id)
  6. tagList(
  7. leafletOutput(ns(&quot;mymap&quot;))
  8. )
  9. }
  10. map_Server &lt;- function(id) {
  11. moduleServer(
  12. id,
  13. function(input, output, session) {
  14. mapdata &lt;- datasets::quakes
  15. mapdata$latitude &lt;- as.numeric(mapdata$lat)
  16. mapdata$longitude &lt;- as.numeric(mapdata$long)
  17. mapdata$id &lt;- 1:nrow(mapdata)
  18. output$mymap &lt;- renderLeaflet({
  19. leaflet(options = leafletOptions(maxZoom = 18)) %&gt;% addTiles() %&gt;%
  20. addMarkers(lat = ~ latitude, lng = ~ longitude,
  21. data = mapdata,
  22. layerId = mapdata$id,
  23. popup= ~paste(&quot;&lt;b&gt;&quot;, mag, &quot;&lt;/b&gt;&lt;/br&gt;&quot;, actionLink(inputId = &quot;modal&quot;, label = &quot;Modal&quot;, onclick = &#39;Shiny.setInputValue(\&quot;button_click\&quot;, this.id, {priority: \&quot;event\&quot;})&#39;)))
  24. })
  25. observeEvent(input$button_click, {
  26. showModal(modalDialog(
  27. title = &quot;TEST MODAL&quot;
  28. )) })
  29. }
  30. )
  31. }
  32. ui &lt;- fluidPage(
  33. map_ui(&#39;ex1&#39;)
  34. )
  35. server &lt;- function(input, output){
  36. map_Server(&#39;ex1&#39;)
  37. }
  38. 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:

  1. function(input, output, session) {
  2. ns <- session$ns
  3. ......
  4. output$mymap <- renderLeaflet({
  5. leaflet(options = leafletOptions(maxZoom = 18)) %>% addTiles() %>%
  6. addMarkers(
  7. lat = ~ latitude, lng = ~ longitude,
  8. data = mapdata,
  9. layerId = mapdata$id,
  10. popup =
  11. ~paste(
  12. "<b>", mag, "</b></br>",
  13. actionLink(
  14. inputId = "modal", label = "Modal",
  15. onclick = sprintf(
  16. 'Shiny.setInputValue("%s", this.id, {priority: "event"})',
  17. ns("button_click")
  18. )
  19. )
  20. )
  21. )
  22. })
  23. ......
  24. }
英文:

Yes, you have to add the ns:

  1. function(input, output, session) {
  2. ns &lt;- session$ns
  3. ......
  4. output$mymap &lt;- renderLeaflet({
  5. leaflet(options = leafletOptions(maxZoom = 18)) %&gt;% addTiles() %&gt;%
  6. addMarkers(
  7. lat = ~ latitude, lng = ~ longitude,
  8. data = mapdata,
  9. layerId = mapdata$id,
  10. popup =
  11. ~paste(
  12. &quot;&lt;b&gt;&quot;, mag, &quot;&lt;/b&gt;&lt;/br&gt;&quot;,
  13. actionLink(
  14. inputId = &quot;modal&quot;, label = &quot;Modal&quot;,
  15. onclick = sprintf(
  16. &#39;Shiny.setInputValue(\&quot;%s\&quot;, this.id, {priority: \&quot;event\&quot;})&#39;,
  17. ns(&quot;button_click&quot;)
  18. )
  19. )
  20. )
  21. )
  22. })
  23. ......
  24. }

huangapple
  • 本文由 发表于 2023年6月2日 03:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定