Exclude all inputs from Shiny bookmarks

Viewed 359

I have a custom bookmark URL for my shiny app. I use setBookmarkExclude() to exclude all inputs (i.e. widgets). Then I use onBookmark() to build a bookmark URL and onRestore() to restore the state.

During development, if new widgets are added, their IDs also have to be added to the setBookmarkExclude() function. If not, then the bookmark URL will change.

Is there a proper way to exclude all inputs?

Initially I tried setBookmarkExclude(names(input)) but this doesn't work since this function is called from inside the application's server function when input is not yet initialized.

Obviously, an opposite function setBookmarkInclude(NULL) would be ideal?

1 Answers

You have already mentioned using setBookmarkExclude(names(input)), which is the right way to go.

The key is to dynamically use setBookmarkExclude wrapped in an observer.

This is a modified version of my answer here showing how to exclude dynamically generated inputs:

library(shiny)

ui <- function(request) {
  fluidPage(
    br(),
    bookmarkButton(id = "bookmarkBtn"),
    actionButton(inputId = "addSlider", label = "Add slider..."),
    hr(),
    textOutput("ExcludedIDsOut"),
    hr(),
    sliderInput(inputId="slider1", label="My value will be bookmarked", min=0, max=10, value=5),
    uiOutput("slider2")
  )
}

server <- function(input, output, session) {

  bookmarkingWhitelist <- c("slider1")

  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })

  ExcludedIDs <- reactiveVal(value = NULL)

  observe({
    toExclude <- setdiff(names(input), bookmarkingWhitelist)
    setBookmarkExclude(toExclude)
    ExcludedIDs(toExclude)
  })

  output$ExcludedIDsOut <- renderText({ 
    paste("ExcludedIDs:", paste(ExcludedIDs(), collapse = ", "))
  })

  observeEvent(input$addSlider, {
    output$slider2 <- renderUI({ 
      sliderInput(inputId="slider2", label="My value will not be bookmarked", min=0, max=10, value=5)
    })
  }, once = TRUE)

}

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