Shiny: Можно ли сохранить вывод в списке? - PullRequest
0 голосов
/ 12 ноября 2019

Я работаю над блестящим приложением для выполнения симуляций и хотел бы сохранить образец из каждой симуляции (то есть список или все, что работает).

Я где-то читал, что могу использовать реактивные значения, но, похоже, это не работает. actual_simulations выглядит как 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)

Прежде чем пытаться использовать реактивные значения, я попытался использовать то, что работает в R, и оно тоже не работает. actual_simulations выглядит как NULL.

actual_simulations <- list() 

Буду признателен за любую помощь в этом. Спасибо.

1 Ответ

0 голосов
/ 12 ноября 2019

Здесь происходит много вещей, которые вызывают у вас проблемы. Я сделаю свои заметки ниже после ## для обсуждения изменений, которые должны быть внесены в линию.

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)
...