【发布时间】:2020-05-20 17:40:59
【问题描述】:
重置单个反应值只需由reactiveVal(NULL) 完成。但是,我怎样才能完全重置reactiveValues()?
虚拟应用程序包含我的一些方法来保留新鲜和干净的反应值,但它们都没有真正做到我希望他们做的事情。此外,观察reactiveValues 时似乎有一种奇怪的行为。除非单击 Trigger 按钮,否则它们不会在清理后触发反应。当我检查他们的状态时,我觉得他们很好。
library(shiny)
library(magrittr)
# UI ---------------------------------------------------------------------------
ui <- fluidPage(
actionButton("create", "Create"),
actionButton("reset", "Reset"),
actionButton("trigger", "Trigger"),
textOutput("out")
)
# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
vals <- reactiveValues()
ids <- reactiveVal()
display <- reactiveVal()
# insert letter when clicked
observeEvent(input$create, {
id <- as.character(length(ids()))
vals[[id]] <- sample(LETTERS, 1)
ids(c(ids(), id))
})
observeEvent(input$reset, {
# Options to reset reactive Values -------------------------------------
vals <<- reactiveValues()
# vals <- NULL
for(i in names(vals)) vals[[i]] <- NULL # deletes content but not the names
# resetting reactiveVal() is easily done via NULL
ids(NULL)
display(NULL)
})
observe({
if(input$trigger) browser()
text <- reactiveValuesToList(vals) %>% paste(collapse = ", ")
display(text)
})
output$out <- renderText(display())
}
shinyApp(ui, server)
P.S.:这个例子没有被完全剥离,因为我希望它反映我的实际反应链。
【问题讨论】:
-
我猜这里的“问题”是重置
reactiveValues对象不会触发任何反应,因为:Note that values taken from the reactiveValues object are reactive, but the reactiveValues object itself is not.(参见?reactiveValues)。相应地,vals发生了更改,但观察者提供display()“不在乎”,因为更改不是反应性的,NULL。 -
啊好吧,这是有道理的!谢谢你指点我这个属性
标签: r shiny shiny-reactivity