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

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

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 &lt;- function(id) {
  ns &lt;- NS(id)
  tagList(
    
    leafletOutput(ns(&quot;mymap&quot;))
    
  )
}

map_Server &lt;- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
      mapdata &lt;- datasets::quakes
      mapdata$latitude &lt;- as.numeric(mapdata$lat)
      mapdata$longitude &lt;- as.numeric(mapdata$long)
      mapdata$id &lt;- 1:nrow(mapdata)
      
      output$mymap &lt;- renderLeaflet({
        leaflet(options = leafletOptions(maxZoom = 18)) %&gt;% addTiles() %&gt;%
          addMarkers(lat = ~ latitude, lng = ~ longitude,
                     data = mapdata,
                     layerId = mapdata$id,
                     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;)))
      })

      observeEvent(input$button_click, {
        showModal(modalDialog(
          title = &quot;TEST MODAL&quot;
        ))      })
      

      
    }
  )
}

ui &lt;- fluidPage(
  
  map_ui(&#39;ex1&#39;)
  
)

server &lt;- function(input, output){
  map_Server(&#39;ex1&#39;)
  
}

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 &lt;- session$ns  
  
  ...... 
  
  output$mymap &lt;- renderLeaflet({
    leaflet(options = leafletOptions(maxZoom = 18)) %&gt;% addTiles() %&gt;%
      addMarkers(
        lat = ~ latitude, lng = ~ longitude,
        data = mapdata,
        layerId = mapdata$id,
        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 = sprintf(
                &#39;Shiny.setInputValue(\&quot;%s\&quot;, this.id, {priority: \&quot;event\&quot;})&#39;,
                ns(&quot;button_click&quot;)
              )
            )
          )
      )
  })
  
  ......
  
}

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:

确定