显然你不能完全取消reactiveValues
看一眼 :
Shiny:如何使用actionButton初始化空的reactiveValues? https://stackoverflow.com/questions/52261240/shiny-how-to-initialize-empty-reactivevalues-with-an-actionbutton
闪亮:如何更新reactiveValues对象? https://stackoverflow.com/questions/26803536/shiny-how-to-update-a-reactivevalues-object
根据这两个问题的答案,作为一种解决方法,可以将列表中的值保存在reactiveValues对象中,您的代码将如下所示:
library(shiny)
library(magrittr)
# UI ---------------------------------------------------------------------------
ui <- fluidPage(
actionButton("create", "Create"),
actionButton("reset", "Reset"),
actionButton("trigger", "Trigger"),
textOutput("out")
)
# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
# initialize vals with a list
vals <- reactiveValues('foo' = list())
ids <- reactiveVal()
display <- reactiveVal()
observeEvent(input$create, {
id <- as.character(length(ids()))
# add values to the list
vals$foo[[id]] <- sample(LETTERS, 1)
ids(c(ids(), id))
})
observeEvent(input$reset, {
# reset the list
vals$foo <- NULL
ids(NULL)
})
observe({
# if(input$trigger) browser()
text <- reactiveValuesToList(vals) %>% paste(collapse = ", ")
display(text)
})
output$out <- renderText({
text <- vals$foo %>% paste(collapse = ", ")
display(text)
display()
})
}
shinyApp(ui, server)