Search code examples
rlistshinysimulationsample

Shiny: Is it possible to store output in a list?


I am working on a shiny app to perform simulations and would like to store the sample from each simulation (i.e. a list or anything that works).

I read somewhere that I could use reactiveValues but it doesn't seem to work. actual_simulations comes out as NULL.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
  sidebarPanel(
    actionButton(inputId = "enter_browser", "Browser", icon = icon("bug"))
  ),
  mainPanel(
    DT::dataTableOutput("last_simulation")
  )
)
)

server <- function(input, output, session){

observeEvent(input$enter_browser, { browser()})

actual_simulations <- reactiveValues()

actual_simulations_data <- reactive({
  for (i in seq_along(1:100)) {
    actual_simulations[['i']] <-
      dplyr::sample_n(tbl = dplyr::as.tbl(mtcars), 
               size = 15,
               replace = TRUE,
               weight = NULL
               )
  }


})

output$last_simulation <- DT::renderDataTable({
  actual_simulations[['100']]
})

}

shinyApp(ui, server)

Before trying with reactiveValues, I attempted what worked in R and it didn't work either. actual_simulations comes out as NULL.

actual_simulations <- list() 

I will appreciate any help on this. Thanks.


Solution

  • Lot's of things going on here that are causing you problems. I'll make my notes below after a ## for in line discussion of changes to be made.

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          # actionButton(inputId = "enter_browser", "Browser", icon = icon("bug"))
        ),
        mainPanel(
          DT::dataTableOutput("last_simulation")
        )
      )
    )
    
    server <- function(input, output, session){
    
      # observeEvent(input$enter_browser, { browser()})
    
      ## You don't need to define this AND actual_simulations_data...pick one.
      # actual_simulations <- reactiveValues()
    
      actual_simulations_data <- reactive({
        ## Preset an object as an empty list so R knows what to do with subsets
        ## This object only exists within this function and is not reactive or global
        actual_simulations <- list()
        for (i in seq_along(1:100)) {
          ## On the next line you want to put data in position i, not position 'i' 
          actual_simulations[[i]] <-
            dplyr::sample_n(tbl = dplyr::as.tbl(mtcars), 
                            size = 15,
                            replace = TRUE,
                            weight = NULL
            )
          ## print(actual_simulations[[i]])
        }
        ## Sometimes with if and for statements there is a problem knowing what to return so be specific
        return(actual_simulations)
      })
    
      output$last_simulation <- DT::renderDataTable({
        ## In order to subset you must first call the reactive using the () 
        ## and you need list item 100 not list item '100'. 
        actual_simulations_data()[[100]]
      })
    
    }
    
    shinyApp(ui, server)