我正在尝试将一些图形模块放在另一个模块中。虽然暂时不工作。我想这不是这样做的方法。关于应该如何做的任何想法?
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