闪亮:是否可以将输出存储在列表中?
Shiny: Is it possible to store output in a list?
我正在开发一个闪亮的应用程序来执行模拟,并希望存储每个模拟的样本(即列表或任何有效的东西)。
我在某处读到我可以使用 reactiveValues 但它似乎不起作用。 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)
在尝试使用 reactiveValues 之前,我尝试了在 R 中有效的方法,但它也没有起作用。 actual_simulations 结果为 NULL。
actual_simulations <- list()
对于这方面的任何帮助,我将不胜感激。谢谢
这里发生的很多事情都会给您带来麻烦。我将在 ##
之后在下面做笔记,以在线讨论要进行的更改。
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)
我正在开发一个闪亮的应用程序来执行模拟,并希望存储每个模拟的样本(即列表或任何有效的东西)。
我在某处读到我可以使用 reactiveValues 但它似乎不起作用。 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)
在尝试使用 reactiveValues 之前,我尝试了在 R 中有效的方法,但它也没有起作用。 actual_simulations 结果为 NULL。
actual_simulations <- list()
对于这方面的任何帮助,我将不胜感激。谢谢
这里发生的很多事情都会给您带来麻烦。我将在 ##
之后在下面做笔记,以在线讨论要进行的更改。
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)