Skip to content

Instantly share code, notes, and snippets.

@strboul
Created June 26, 2018 16:45
Show Gist options
  • Save strboul/92157a72dc46e23275b9720fdbd0d1f4 to your computer and use it in GitHub Desktop.
Save strboul/92157a72dc46e23275b9720fdbd0d1f4 to your computer and use it in GitHub Desktop.
Shiny record past input values
#' Record the past input values 'only for a session'. Persistent store approach is different.
#' This snippet has example for selecting rows in datatable from the DT package. Reference:
#' https://stackoverflow.com/a/41199134
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("dt")
)
server <- function(input, output, session) {
output$dt <- renderDataTable(mtcars, selection = list(mode = 'single'))
row <- reactiveValues(pastval = 0, curval = 0)
observeEvent(input$dt_rows_selected, {
row$prevval <- row$curval
row$curval <- input$dt_rows_selected
})
row.current <- reactive({
req(input$dt_rows_selected)
input$dt_rows_selected
row$curval
})
row.prev <- reactive({
req(input$dt_rows_selected)
input$dt_rows_selected
row$prevval
})
# print debug:
observe({
print(paste("input.row:", input$dt_rows_selected))
print(sprintf("row.current:%d", row.current()))
print(sprintf("row.prev:%d", row.prev()))
print(paste("---"))
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment