2

How to retain previous input in Shiny?

I want to show how estimates change according to user input.

E.g., If user changes input and an estimate is up, then in some panel I want to print that estimates is up.

To do so, I want to get sequence of user input such as

> c(2,4,5,6)
[1] 2 4 5 6

where 2,4,5,6 is previous inputs obtained by sliderInput. That is, first, user chose 2, second chosen number is4,..and so on.


Edit

The following is the ansewer of @GyD.

    library(shiny)
    # Define UI for application that draws a histogram
    ui <- fluidPage(

        # Application title
        titlePanel("Old Faithful Geyser Data"),

        # Sidebar with a slider input for number of bins 
        sidebarLayout(
            sidebarPanel(
                sliderInput("bins",
                            "Number of bins:",
                            min = 1,
                            max = 50,
                            value = 30)
            ),

            # Show a plot of the generated distribution
            mainPanel(
               verbatimTextOutput("print")
            )
        )
    )

    # print history of user input
    server <- function(input, output) {

        rv <- reactiveValues(prev_bins = NULL)
        observeEvent(input$bins, {
# If event occurs, then run the following append function
            rv$prev_bins <- c(rv$prev_bins, input$bins)
        })

        # Output
        output$print <- renderPrint({
            paste(rv$prev_bins, collapse = ",")
        })

        # output$print <- renderPrint({
        #    
        #     paste(s, input$bins,sep = ",")
        # })
    }

    # Run the application 
    shinyApp(ui = ui, server = server)
Camford Oxbridge
  • 834
  • 8
  • 21

1 Answers1

5

You could store the previous and actual values inside a reactiveValues object:

rv$prev_bins is initialized as NULL, then on every value change, the new value is appended to the vector.

To keep only the previous and current values instead of all, use: rv$prev_bins <- c(tail(rv$prev_bins, 1), input$bins).

Code:

# Initialize reactive values
rv <- reactiveValues(prev_bins = NULL)

# Append new value to previous values when input$bins changes 
observeEvent(input$bins, {
  rv$prev_bins <- c(rv$prev_bins, input$bins)
})

# Output
output$print <- renderPrint({
  paste(rv$prev_bins, collapse = ",")
})

Output:

Output

GyD
  • 3,902
  • 2
  • 18
  • 28
  • Thank you, GyD, I understand it. First, create an object in `reactive Values` and when event occurs, then the function in `observeEvent` runs and it changes object in `reactiveValues`. So, any object should be written in the `reactiveValues`. I learn a lot from you. Thank you. I will try various applications of your code, thank you GyD !! – Camford Oxbridge Jul 09 '19 at 12:24
  • 1
    @CamfordOxbridge, glad to help! the key here is that you can modify `reactiveValues` within `observe` and `observeEvent`, unlike `reactives`. – GyD Jul 09 '19 at 13:09
  • Thank you @GyD. Your answer is very plain!! I feel that I could do various things using your key idea, thank you very much , @GyD!!! – Camford Oxbridge Jul 09 '19 at 13:22