How to call a module inside a module when there is a global variable? #1101
-
I want to use a module inside another module, when there is no global variable, it works. See demo code below: library(golem)
library(shiny)
#1. Define the inner module
inner_module_ui <- function(id) {
ns <- NS(id)
fluidPage(
h1("Inner Module"),
selectInput(inputId = ns("colName"),
label = "Select a column name",
choices = NULL,
selected = NULL
)
)
}
inner_module_server <- function(id, uploadedData) {
moduleServer(id, function(input, output, session) {
observeEvent(uploadedData, {
updateSelectInput(session = session,
inputId = "colName",
choices = names(uploadedData),
selected = names(uploadedData)[1]
)
})
})
}
#2. Define the outer module
outer_module_ui <- function(id) {
ns <- NS(id)
fluidPage(
h1("Outer Module"),
fileInput(ns("file"), "Choose a CSV file", accept = ".csv"),
tableOutput(ns("contents")),
inner_module_ui(ns("inner_module"))
)
}
outer_module_server <- function(id) {
moduleServer(id, function(input, output, session) {
data <- reactive({
req(input$file)
read.csv(input$file$datapath, header = TRUE)
})
output$contents <- renderTable({
data()
})
inner_module_server("inner_module", uploadedData = data())
})
}
#3. Define the main app
app_server <- function(input, output, session) {
outer_module_server("outer_module")
}
app_ui <- function() {
navbarPage(
title = "demo",
tabPanel("Outer Module", outer_module_ui("outer_module"))
)
}
#4. Run app
shinyApp(app_ui, app_server) When I introduce a global variable, the inner module does not work any longer: library(golem)
library(shiny)
#1. Define the inner module
inner_module_ui <- function(id) {
ns <- NS(id)
fluidPage(
h1("Inner Module"),
selectInput(inputId = ns("colName"),
label = "Select a column name",
choices = NULL,
selected = NULL
)
)
}
inner_module_server <- function(id, uploadedData, global) {
moduleServer(id, function(input, output, session) {
observeEvent(uploadedData, {
updateSelectInput(session = session,
inputId = "colName",
choices = names(uploadedData),
selected = names(uploadedData)[1]
)
})
})}
#2. Define the outer module
outer_module_ui <- function(id) {
ns <- NS(id)
fluidPage(
h1("Outer Module"),
fileInput(ns("file"), "Choose a CSV file", accept = ".csv"),
tableOutput(ns("contents")),
inner_module_ui(ns("inner_module"))
)
}
outer_module_server <- function(id, global) {
moduleServer(id, function(input, output, session) {
observe({
req(input$file)
global$Data <- read.csv(input$file$datapath, header = TRUE)
})
output$contents <- renderTable({
global$Data
})
inner_module_server("inner_module", uploadedData = global$Data, global = global)
})
}
#3. Define the main app
app_server <- function(input, output, session) {
global <- reactiveValues(
Data = NULL
)
outer_module_server("outer_module", global = global)
}
app_ui <- function() {
tabPanel("Outer Module", outer_module_ui("outer_module"))
}
#4. Run app
shinyApp(app_ui, app_server) I don't know where the problem is. Thanks a lot for your help. Dong |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @YonghuiDong I was able to reproduce your example inside a clean https://github.com/ilyaZar/TestGolem I fixed some errors along, mostly passing arguments correctly from module to module. Have a look at the specific git-commits, the comments show what I added in a historic sequence. It does not match your example exactly, because it's a golem-based R package but maybe this helps finding you the error (whatever your true code-base is; I assume that the above is a simplified reprex). Let me know if it works for you or post the exact error message that could be useful too. Remark:: |
Beta Was this translation helpful? Give feedback.
Hey @YonghuiDong
I was able to reproduce your example inside a clean
golem
-skeleton package, find the repo here:https://github.com/ilyaZar/TestGolem
I fixed some errors along, mostly passing arguments correctly from module to module. Have a look at the specific git-commits, the comments show what I added in a historic sequence.
It does not match your example exactly, because it's a golem-based R package but maybe this helps finding you the error (whatever your true code-base is; I assume that the above is a simplified reprex).
Let me know if it works for you or post the exact error message that could be useful too.
Remark::
Not directly related to your problem, but still you may have a l…