I understand that the callModule()
function in R Shiny is outdated syntax, and session$userData
should instead be used for inter-module communication. I have tried converting the below example code to using session$userData
but keep getting error message like this one:
Error in moduleServer(id, function(input, output, session) { :
unused argument (function(input, output, session) {
observe({
session$userData$mod1Value <- session$userData$sharedValue() + 10
})
output$result1 <- renderText({
paste("Mod 1 Result = base + 10:", session$userData$mod1Value)
})
})
How do I replace callModule()
with session$userData
, in the code shown at the bottom of this post?
Oddly, I tried running the accepted answer in post Shiny: How to pass an input from one module to another? but get a similar error message of:
Warning: Error in moduleServer: unused argument (function(input, output, session) {
innerModule <- inner_module_server("inner_module")
output$OOP <- renderPrint({
innerModule()
})
})
50: outer_module_server [#2]
49: server [#2]
3: runApp
2: print.shiny.appobj
1: <Anonymous>
Error in moduleServer(id, function(input, output, session) { :
unused argument (function(input, output, session) {
innerModule <- inner_module_server("inner_module")
output$OOP <- renderPrint({
innerModule()
})
})
Here is the code I am trying to use session$userData
with. Note that I have 2 modules that share data to serve as a template for my modules:
library(shiny)
mod1_ui <- function(id) {
ns <- NS(id)
textOutput(ns("result1"))
}
mod1_server <- function(input, output, session, common) {
output$result1 <- renderText({
common$mod1Value <- common$sharedValue + 10
paste("Mod 1 Result = base + 10:", common$mod1Value)
})
}
mod2_ui <- function(id) {
ns <- NS(id)
textOutput(ns("result2"))
}
mod2_server <- function(input, output, session, common) {
output$result2 <- renderText({
req(common$mod1Value)
paste("Mod 2 Result = Mod 1 Result * 2:", common$mod1Value * 2)
})
}
ui <- fluidPage(
mainPanel(
h5(strong("Base value:")),
sliderInput("svc", "", min = 0, max = 10, value = 0),
mod1_ui("mod1"),
mod2_ui("mod2")
)
)
server <- function(input, output, session) {
common <- reactiveValues(sharedValue = 0)
observeEvent(input$svc, { common$sharedValue <- input$svc })
callModule(mod1_server, "mod1", common = common)
callModule(mod2_server, "mod2", common = common)
}
shinyApp(ui, server)