Reputation: 1708
I am trying to put some graph modules inside another module. Not working at the moment though. I imagine that this isn't the way to do it. Any ideas on how it should be done?
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)
Note: also posted here: https://community.rstudio.com/t/modules-within-modules-graph-in-other-modules/115160
Upvotes: 1
Views: 987
Reputation: 5109
Your issue right now is in:
# brings graphs in to display
outer_server("mod1",
graph_server$inner1, # from graph server
graph_server$inner2) # from graph server
graph_server
is a function, so you can't subset it with $
.
The other issue is that you call graph_server at the wrong module level.
Your currently doing (in terms of nesting) :
L app_ui
L outer_ui
L graph_ui
L app_server
L outer_server
L graph_server
As you can see, the graph module parts are not at the same depth.
You should aim at:
L app_ui
L outer_ui
L graph_ui
L app_server
L outer_server
L graph_server
Here's the working structure :
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)
Cheers, Colin
Upvotes: 3