4

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?

Davor Josipovic
  • 5,296
  • 1
  • 39
  • 57
  • 1
    Please check the whitelist approach I provided [here](https://stackoverflow.com/a/55825515/9841389). Even though I'm not sure if it helps in your specfic case since you haven't provided any example. – ismirsehregal Jun 21 '19 at 09:38
  • @ismirsehregal, your answer gave me the idea to do it this way: `observe({setBookmarkExclude(names(input))}, priority = 100, autoDestroy = TRUE)` which works. From documentation I thought I had to place `setBookmarkExclude()` in the server function to be executed during initialisation. Seems not so. You can post your answer if you want. – Davor Josipovic Jun 21 '19 at 09:52

1 Answers1

4

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)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78