如何在shiny app中链接两个模块(一个使用reactable,另一个使用leaflet)?

bejyjqdl  于 2023-06-27  发布在  React
关注(0)|答案(1)|浏览(115)

我有一个应用程序,使用reactable渲染一些数据,并在Map中显示相同的数据(使用传单)。
表格对象是可选择的,并且在Map中示出所选择的行。
当我清除表中的选择(使用actionButton)时,我希望渲染原始Map(所有数据点)。
下面是一个地震数据集的工作示例。

library(shiny)
library(leaflet)
library(reactable)

ui <- fluidPage(
  reactableOutput("t"),
  actionButton(inputId = "clean", label="Clear selection"),
  leafletOutput("m")
)

server <- function(input, output) {
  output$t <- renderReactable({
    reactable(quakes[1:10,],
    selection = "multiple")
  })
  output$m <- renderLeaflet({
    leaflet(quakes[1:100,]) %>%
      addTiles() %>%
      addMarkers()
  })
  
  observeEvent(input$clean, 
             list(  updateReactable("t", selected = NA),
               leafletProxy("m", data = quakes) %>%
                 addMarkers())
  )
  
  observeEvent(getReactableState("t","selected"),
     leafletProxy("m", data = quakes[getReactableState("t","selected"),]) %>%
       clearMarkers() %>%
       addMarkers()
   ) 
}
shinyApp(ui,server)

当我尝试使用模块编写相同的应用程序时,它不起作用。下面是众多尝试之一。
我正在尝试使用模块,因为这是一个更大的应用程序的一部分,它会使它更容易管理...

library(shiny)
library(leaflet)
library(reactable)

tUI <- function(id){
  ns <- NS(id)
  tagList(
    reactableOutput(outputId = ns("t")),
    actionButton(ns("clean"), label="Clear selection")
  )
}
tServer <- function(id, mapa){
  moduleServer(id,
      function(input, output, session){
        output$t <- renderReactable({
          reactable(quakes[1:10,],
                    selection = "multiple"
          )
        })
        
        observeEvent(input$clean, {
          list(
            updateReactable("t", selected = NA)#,
         #   leafletProxy(mapa(), data = quakes) %>%
         #     addMarkers())
        )})
      }
  )
}

mUI <- function(id){
  ns <- NS(id)
  tagList(
    leafletOutput(ns("m"))
  )
}
mServer <- function(id){
  moduleServer(id,
               function(input, output, session){
                  output$m <- renderLeaflet({
                    leaflet(quakes[1:100,]) %>%
                     addTiles() %>%
                     addMarkers()
                 })
                  m2 <- reactive("m")
  })}
##########################################################
ui <- fluidPage(
  tUI("tt"),
  mUI("mm")
)

server <- function(input, output) {
  ttt <- tServer("tt", mmm$m2)
  mmm <- mServer("mm")
}

shinyApp(ui,server)

有什么需要帮忙的吗?谢谢

e0bqpujr

e0bqpujr1#

为了在模块之间进行交互,您需要从一个模块返回React对象并将其输入到另一个模块。这里,我从tServer模块返回了cleangetReactableState,并将其输入到mServer。对于这个概念的介绍,您可能会发现my tutorial on shiny modules很有帮助。

library(shiny)
library(leaflet)
library(reactable)

tUI <- function(id){
  ns <- NS(id)
  tagList(
    reactableOutput(outputId = ns("t")),
    actionButton(ns("clean"), label="Clear selection")
  )
}
tServer <- function(id, mapa){
  moduleServer(id,
               function(input, output, session){
                 output$t <- renderReactable({
                   reactable(quakes[1:10,],
                             selection = "multiple"
                   )
                 })
                 
                 observeEvent(input$clean, {
                   list(
                     updateReactable("t", selected = NA)
                   )})
                 
                 return(
                   list(
                     clean = reactive({input$clean}),
                     selected_items = reactive({getReactableState("t","selected")})
                   )
                 )
               }
  )
}

mUI <- function(id){
  ns <- NS(id)
  tagList(
    leafletOutput(ns("m"))
  )
}
mServer <- function(id, clean, selected_items){
  moduleServer(id,
               function(input, output, session){
                 output$m <- renderLeaflet({
                   leaflet(quakes[1:100,]) %>%
                     addTiles() %>%
                     addMarkers()
                 })
                 
                 observeEvent(clean(), {
                   
                   leafletProxy("m", data = quakes) %>%
                     addMarkers()
                 }
                 )
                 
                 observeEvent(selected_items(), {
                   leafletProxy("m", data = quakes[selected_items(),]) %>%
                     clearMarkers() %>%
                     addMarkers()
                 })
                 
               })}
##########################################################
ui <- fluidPage(
  tUI("tt"),
  mUI("mm")
)

server <- function(input, output) {
  ttt <- tServer("tt")
  mServer("mm", clean = ttt$clean, selected_items = ttt$selected_items)
}

shinyApp(ui,server)

相关问题