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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…