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

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

How to link two modules in shiny app (one using reactable and the other leaflet)?

问题

我已经将代码部分移除,以下是翻译好的部分:

  1. 在一个应用程序中,使用reactable渲染一些数据,并在地图中显示相同的数据(使用leaflet)。
  2. 表格对象是可选择的,所选行在地图中显示。
  3. 当我在表格中清除选择(使用actionButton),我希望原始地图重新渲染(显示所有数据点)。

下面是一个使用quakes数据集的工作示例。

当我尝试使用模块编写相同的应用程序时,它不起作用。以下是其中的一次尝试。

我尝试使用模块,因为这是一个更大应用程序的一部分,使用模块可以更容易管理。

请问还有其他需要帮助的内容吗?

英文:

I have an app that renders some data using reactable and shows the same data in a map (using leaflet).

the table object is selectable and the selected rows are shown in the map.

when I clear the selection in the table (with an actionButton), I want the original map to render (with all data points).

Below is a working example with the quakes dataset.

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)

When I try to write the same app with modules, it does not work. Below is one of the many attempts.

I am trying to use modules, since this is part of a larger app, and it would make it much easier to manage...

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)

Any help, please?
Thanks.

答案1

得分: 2

为了实现模块之间的交互,您需要从一个模块返回反应性对象,并将其输入到另一个模块中。在这里,我从"tServer"模块返回了cleangetReactableState,然后将其输入到了"mServer"模块中。关于这个概念的介绍,您可以在我的Shiny模块教程中找到有帮助的信息。

英文:

In order to have cross-talk between modules, you need to return reactive objects from one module and input it to the other module. Here, I've returned clean and getReactableState from the tServer module and input it to mServer. For an introduction to this concept, you might find my tutorial on shiny modules helpful.

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)

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

发表评论

匿名网友

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

确定