Reuse an existing Shiny Bookmark folder on server with new state input.rds and values.rds

Hello helpful Shiny friends,

I have users who embed Shiny Bookmarks links in their web pages. Sometimes the bookmark they've made needs an edit. I want override the newly created state$dir variable with the original link's directory. I know I can overwrite the input.rds and values.rds with some custom code but I'd like to take advantage of the machinery already built in the bookmark functions to compose those config files properly. Included is altered example from Shiny site. I've added code to the onBookmark() where I'd like to be able to make the change. Thanks in advance for any time you spend on this question.

print(packageVersion('shiny'))
[1] ‘1.1.0’

library(shiny)
options(shiny.fullstacktrace = TRUE)
options(shiny.trace = TRUE)


ui <- function(request) {
  fluidPage(
    sidebarPanel(
      sliderInput("n", "Value to add", min = 0, max = 100, value = 50),
      actionButton("add", "Add"), br(), br(),
      bookmarkButton()
    ),
    mainPanel(
      h4("Sum of all previous slider values:", textOutput("sum"))
    )
  )
}

server <- function(input, output, session) {
  vals <- reactiveValues(sum = 0)
  
  #### Save extra values in state$values when we bookmark
  onBookmark(
    function(state) {
    print (session$dir)
    dir_root <- '/user/'
    dir <- '67acc0cf129049c9'
    session$dir <- paste0(dir_root,dir)
  })
  
  #### Read values from state$values when we restore

onRestore(function(state) {
  vals$sum <- state$values$currentSum
})

#### Exclude the add button from bookmarking
setBookmarkExclude("add")

observeEvent(input$add, {
  vals$sum <- vals$sum + input$n
})
output$sum <- renderText({
  vals$sum
})
}

shinyApp(ui, server, enableBookmarking = "server")
> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          3.2                         
year           2016                        
month          10                          
day            31                          
svn rev        71607                       
language       R                           
version.string R version 3.3.2 (2016-10-31)
nickname       Sincere Pumpkin Patch

If I correctly understand, you want to set the path for the RDS files created on bookmarking. This can be achieved with the help of the save.interface and load.interface options:

library(shiny)

saveInterface <- function(id, callback){
  stateDir <- file.path("/user", "67acc0cf129049c9", id)
  if(!dir.exists(stateDir)) dir.create(stateDir, recursive = TRUE)
  callback(stateDir)
}
loadInterface <- function(id, callback){
  stateDir <- file.path("/user", "67acc0cf129049c9", id)
  if(!dir.exists(stateDir)) dir.create(stateDir, recursive = TRUE)
  callback(stateDir)
}

shinyOptions(save.interface = saveInterface, load.interface = loadInterface)
1 Like