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)
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).
# 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 = ",")
})

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With