Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny - How to remember user input after click on refresh?

Tags:

r

shiny

I've this Shiny app that ask an input from the user. how can I hold the user input also after refresh the page? for example - if the user write "hello" I'll see his input. I want still see his last input even if he clicks on refresh

## Only run examples in interactive R sessions
if (interactive()) {
  
  ui <- fluidPage(
    textInput("caption", "Caption", "Data Summary"),
    verbatimTextOutput("value")
  )
  server <- function(input, output) {
    output$value <- renderText({ input$caption })
  }
  shinyApp(ui, server)
}
like image 551
asdf1212 Avatar asked Jan 24 '26 16:01

asdf1212


1 Answers

A native shiny approach is "live" bookmarking - please see ?updateQueryString

library(shiny)

ui = function(req) {
  fluidPage(
    textInput("caption", "Caption", "Data Summary"),
    verbatimTextOutput("value")
  )
}

server = function(input, output, session) {
  observe({
    # Trigger this observer every time an input changes
    reactiveValuesToList(input)
    session$doBookmark()
  })
  onBookmarked(function(url) {
    updateQueryString(url)
  })
  output$value <- renderText({ input$caption })
}

shinyApp(ui, server, enableBookmarking = "url")

Another approach would be to use use shiny's onSessionEnded callback to save the current state of the inputs e.g. via save() and load() them on session start - or using library(shinyStore) to save the inputs in the client browser's local storage:

# install.packages("devtools")
# library(devtools)
# install_github("trestletech/shinyStore")

library(shiny)
library(shinyStore)

ui <- fluidPage(
  initStore("store", "myUniqueNamespace"),
  textInput("caption", "Caption", "Data Summary"),
  verbatimTextOutput("value")
)

server <- function(input, output, session) {
  observeEvent(input$store$caption, {
    freezeReactiveValue(input, "caption")
    updateTextInput(session, "caption", value = input$store$caption)
  })
  
  observe({
    updateStore(session, "caption", input$caption)
  })
  
  output$value <- renderText({ input$caption })
}
shinyApp(ui, server)
like image 149
ismirsehregal Avatar answered Jan 26 '26 09:01

ismirsehregal



Donate For Us

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