如何重置反应值?
How to reset reactiveValues?
重置单个反应值只需 reactiveVal(NULL)
即可完成。但是,如何才能完全重置 reactiveValues()
?
虚拟应用程序包含我的一些方法来保留新鲜和干净的反应值,但 none 它们确实做了我希望它们做的事情。此外,观察 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
看看:
Shiny: How to initialize empty reactiveValues with an actionButton?
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)
重置单个反应值只需 reactiveVal(NULL)
即可完成。但是,如何才能完全重置 reactiveValues()
?
虚拟应用程序包含我的一些方法来保留新鲜和干净的反应值,但 none 它们确实做了我希望它们做的事情。此外,观察 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
看看:
Shiny: How to initialize empty reactiveValues with an actionButton?
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)