Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

r - Previous input in Shiny

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...