11.6 Updating the URL

It is probably more convenient to have the URL just update itself whenever inputs change instead of having the user press button each time a state must be captured.

# Automatically bookmark every time an input changes
observe({
  reactiveValuesToList(input)
  session$doBookmark()
})

# Update the query string
onBookmarked(updateQueryString)
server code (updated)
server <- function(input, output, session) {
  t <- reactive(seq(0, input$length, length = input$length * 100))
  x <- reactive(sin(input$omega * t() + input$delta) * input$damping ^ t())
  y <- reactive(sin(t()) * input$damping ^ t())

  output$fig <- renderPlot({
    plot(x(), y(), axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 2)
  }, res = 96)
  
  # Automatically bookmark every time an input changes
  observe({
    reactiveValuesToList(input)
    session$doBookmark()
  })
  
  # Update the query string
  onBookmarked(updateQueryString)
}
  • reactiveValuesToList(input): takes the reactive object, input and stores its values and dependencies in a list (i.e. analogous to as.list())

  • doBookmark() invokes the onBookmarked() callback function from the session object.

    • A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.
    • The session object is an environment that can be used to access information and functionality relating to the session.