R 闪亮:可以将输出存储在列表中吗?
我正在开发一个闪亮的应用程序来执行模拟,并希望存储每个模拟的样本(即列表或任何有效的东西) 我在某个地方读到,我可以使用反应值,但它似乎不起作用。实际的模拟结果为空R 闪亮:可以将输出存储在列表中吗?,r,list,shiny,simulation,sample,R,List,Shiny,Simulation,Sample,我正在开发一个闪亮的应用程序来执行模拟,并希望存储每个模拟的样本(即列表或任何有效的东西) 我在某个地方读到,我可以使用反应值,但它似乎不起作用。实际的模拟结果为空 library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( actionButton(inputId = "enter_browser", "Browser", icon = icon("bug")) ), mainPanel( D
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)
actual_simulations <- list()
库(闪亮)
ui这里发生的很多事情都会给你带来麻烦。在##
之后,我将在下面做笔记,以便在线讨论将要进行的更改
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)
库(闪亮)
ui如果您包含一个简单的示例输入和所需的输出,可以用来测试和验证可能的解决方案,那么它将更容易帮助您。使您的代码能够运行,以便可以对其进行测试。您是否需要在闪亮的应用程序终止后保留此列表?这正是我需要的。非常感谢。