error returning multiple module outputs to main app

This should be pretty straight forward for most people. I'm trying to return two data sets from a shiny module for use in a main app. Below is a contrived example. I'd like to to return iris and mtcars and then just display mtcars in my example in the main app. I have a reference dataset of BOD in the module's UI as well as just a base case. Any advice would be much appreciated!


mtcarsUI <- function(id) {
  ns <- NS(id)
  tagList(
    verbatimTextOutput(ns('out'))
  )
}

mtcarsServer <- function(id) {
      moduleServer(id, function(input, output, session) {
        
          output$out <- renderPrint(BOD)
          
          # This isn't being returned properly
          list(mtcars_data= reactive({mtcars}),
               iris_data = reactive({iris})
          )
      })
}

mtcarsApp  <- function() {
    ui <- fluidPage(
        mtcarsUI('test'),
        verbatimTextOutput('mtcars_out')
    )
    
    server <- function(input, output, session) {
        datasets <- mtcarsServer('test')
        
        # if I set 'datasets' as a reactive i.e. datasets() I get an error.
        # If I keep 'datasets' as is, it returns that it's a reactive object, but not the value
        output$mtcars_out <- renderPrint(datasets()$mtcars_data)
    }
    
    shinyApp(ui, server)    
}

mtcarsApp()

I think you should do this

 reactive({list(mtcars_data= mtcars,
         iris_data = iris)})

Thanks so much! This worked. I think it's confusing because in Mastering Shiny, section 19.3.6, Hadley returns a list and each element is a reactive rather than making the entire list reactive.

ok well, you can keep

 list(mtcars_data= reactive({mtcars}),
         iris_data = reactive({iris})
    )

but then your server code must change

    output$mtcars_out <- renderPrint(datasets$mtcars_data())

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.