【问题标题】:How to reset reactiveValues?如何重置reactiveValues?
【发布时间】: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


【解决方案1】:

显然你不能完全取消 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2013-12-11
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多