【问题标题】:Shiny modules inside other modules其他模块中的闪亮模块
【发布时间】:2021-11-09 07:46:55
【问题描述】:

我正在尝试将一些图形模块放入另一个模块中。虽然暂时不工作。我想这不是这样做的方法。关于应该如何完成的任何想法?

library(shiny)
library(tidyverse)
library(palmerpenguins)

# modules -----------------------------------------------------------------

# module that creates graphs
graph_ui <- function(id) {
  ns <- NS(id)
  plotOutput(ns("graph"))
}

# ui that brings in graphs from other module
outer_ui <- function(id) {
  ns <- NS(id)
  tagList(
    "some text - would contain other objects too, not just graphs",
    uiOutput(ns("graph1")), # output from renderUI from graph server
    uiOutput(ns("graph2")) # output from renderUI from graph server
  )
}

# creates graphs
graph_server <- function(id, xcat, ycat) {
  moduleServer(id, function(input, output, session) {
    
      output$graph <- renderPlot({
        
        ggplot(penguins, aes(.data[[xcat]], .data[[ycat]], col = sex)) +
          geom_point()
      })
    }
  )
}

# brings in graphs
outer_server <- function(id, plot1, plot2) {
  moduleServer(
    id, function(input, output, session) {
      output$graph1 <- renderUI(graph_server("inner1")) # from graph server
      output$graph2 <- renderUI(graph_server("inner2")) # from graph server
      
    }
  )
}

# app ---------------------------------------------------------------------
ui <- fluidPage(
  outer_ui("mod1")
)

server <- function(input, output, session) {
  
  # graphs
  graph_server("inner1", xcat = "bill_length_mm", ycat = "bill_depth_mm")
  graph_server("inner2", xcat = "flipper_length_mm", ycat = "bill_depth_mm")
  
  # brings graphs in to display
  outer_server("mod1",
               graph_server$inner1, # from graph server
               graph_server$inner2) # from graph server
}
shinyApp(ui, server)

注意:也在这里发布:https://community.rstudio.com/t/modules-within-modules-graph-in-other-modules/115160

【问题讨论】:

    标签: r shiny module shinymodules


    【解决方案1】:

    你现在的问题是:

      # brings graphs in to display
      outer_server("mod1",
                   graph_server$inner1, # from graph server
                   graph_server$inner2) # from graph server
    

    graph_server 是一个函数,因此您不能使用$ 对其进行子集化。

    另一个问题是您在错误的模块级别调用 graph_server。

    您目前正在做的事情(在嵌套方面):

    L app_ui
      L outer_ui
        L graph_ui
    L app_server
      L outer_server
      L graph_server
    

    如您所见,图形模块部分不在同一深度。

    你应该瞄准:

    L app_ui
      L outer_ui
        L graph_ui
    L app_server
      L outer_server
        L graph_server
    

    这是工作结构:

    library(shiny)
    library(tidyverse)
    library(palmerpenguins)
    
    # Start with the lower level, just plotOutput & renderPlot
    graph_ui <- function(id) {
      ns <- NS(id)
      plotOutput(ns("graph"))
    }
    
    graph_server <- function(id, xcat, ycat) {
      moduleServer(id, function(input, output, session) {
        output$graph <- renderPlot({
          ggplot(
            penguins, 
            aes(
              .data[[xcat]], 
              .data[[ycat]], 
              col = sex
            )
          ) +
            geom_point()
        })
      }
      )
    }
    
    # Here is a container for the graph module
    outer_ui <- function(id) {
      ns <- NS(id)
      tagList(
        "some text - would contain other objects too, not just graphs",
        graph_ui(ns("graph1")),  # First use of the graph module
        graph_ui(ns("graph2")) # Second use of the graph module
      )
    }
    
    
    # Server counterpart
    outer_server <- function(id) {
      moduleServer(id, function(input, output, session) {
          # Making the graphs happen
          graph_server("graph1", xcat = "bill_length_mm", ycat = "bill_depth_mm")
          graph_server("graph2", xcat = "flipper_length_mm", ycat = "bill_depth_mm")
          
        }
      )
    }
    
    # app ---------------------------------------------------------------------
    ui <- fluidPage(
      outer_ui("mod1")
    )
    
    server <- function(input, output, session) {
      outer_server("mod1")
    }
    shinyApp(ui, server)
    

    干杯, 科林

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2021-07-17
      • 2017-12-09
      • 2018-05-11
      • 2023-04-09
      • 1970-01-01
      • 2021-01-10
      • 2020-09-30
      • 2019-01-18
      相关资源
      最近更新 更多