The below example code uses a module structure to pass values from the main server to two modules mod1
and mod2
where the values are manipulated and passed back and forth between the modules. This is for learning purposes only. Trying to get my head around transmitting data between modules.
I am trying to send the complete common
object (which I thought is reactive) to the global environment for examining its contents as it evolves. I use the line of code observe({assign("common_tmp", common, envir = .GlobalEnv)})
in the main server section below, and it returns the following which tells me little:
> common_tmp
<ReactiveValues>
Values: colA_mod2, df, mod1_dat, mod2_table, svc
Readonly: FALSE
How do I send the complete common
object to the global environment for examination? So all the contents and embedded values are shown.
Code:
library(shiny)
library(DT)
mod1_ui <- function(id) {
ns <- NS(id)
DTOutput(ns("tbl"))
}
mod1_server <- function(id, common) {
moduleServer(id, function(input, output, session) {
new_dat <- reactive({
df <- common$df
svc <- common$svc()
df * as.numeric(svc)
})
observe({
common$mod1_dat <- new_dat()
common$colA_mod2 <- common$mod2_table$A
})
output$tbl <- renderDT({
dat <- new_dat()
dat$colA_mod2 <- common$colA_mod2
dat
})
})
}
mod2_ui <- function(id) {
ns <- NS(id)
DTOutput(ns("tbl"))
}
mod2_server <- function(id, common) {
moduleServer(id, function(input, output, session) {
output$tbl <- renderDT({
mod1_dat <- common$mod1_dat
dat <- data.frame(mod1_dat + 10)
if (!is.null(common$colA_mod2)) {
dat$colA_mod2_mod1 <- common$colA_mod2 * 100
}
common$mod2_table <- dat
dat
})
})
}
ui <- fluidPage(
mainPanel(
DTOutput("table"),
sliderInput("svc", "", min = 0, max = 10, value = 1),
mod1_ui("mod1"),
mod2_ui("mod2")
)
)
server <- function(input, output, session) {
common <- reactiveValues(df = data.frame(A = 1:2, B = 3:4))
output$table <- renderDT({datatable(common$df)})
common$svc <- reactive(input$svc)
mod1_server("mod1", common)
mod2_server("mod2", common)
observe({assign("common_tmp", common, envir = .GlobalEnv)})
}
shinyApp(ui, server)
Following up on stefan's recommendation, adding the following to the main server section seems does the trick (this is a temporary function as I build out the App, I've seen that assigning to the global environment with <<-
is considered sloppy):
observe({common_tmp <<- shiny::reactiveValuesToList(common)})
Then I can see the entire contents of common
at my convenience, in R studio console. The object common
, in the full App, gets very long and convoluted and I need to be able to review it easily, download it to XLS for testing, etc.