Can I save the old value of a reactive object when it changes?

后端 未结 2 2151
不思量自难忘°
不思量自难忘° 2021-02-13 04:56

Note: After coming up with the answer I reworded the question to make if clearer.

Sometimes in a shiny app. I want to make use of a value s

2条回答
  •  猫巷女王i
    2021-02-13 05:06

    Seeing as the session flush event method seems to be broken for this purpose, here is an alternative way to do it using an observeEvent construct and a reactive variable.

    library(shiny)
    
    ui <- fluidPage(
      h1("Memory"),
      sidebarLayout(
        sidebarPanel(
          numericInput("val", "Next Value", 10)
        ),
        mainPanel(
          verbatimTextOutput("curval"),
          verbatimTextOutput("lstval")
        )
      )
    )
    
    server <- function(input,output,session) {
      rv <- reactiveValues(lstval=0,curval=0)
    
      observeEvent(input$val, {rv$lstval <- rv$curval; rv$curval <- input$val})
    
      curre <- reactive({req(input$val);  input$val; rv$curval})
      lstre <- reactive({req(input$val);  input$val; rv$lstval})
    
      output$curval <- renderPrint({sprintf("cur:%d",curre())})
      output$lstval <- renderPrint({sprintf("lst:%d",lstre())})
    }
    options(shiny.reactlog = TRUE)
    shinyApp(ui, server)
    

    Yielding:

提交回复
热议问题