Graham
Graham

Reputation: 176

R shiny bookmark bucket lists

I am trying to achieve persistent data storage of R Shiny inputs via a bookmarking method. However, I am unable to retain data related to bucket lists.

If, for example, I begin with the following code, and transfer n items to the second bucket list, the input is not stored. How could I rectify this? Thank you.

library(shiny)

ui <-   fluidPage( bucket_list(
  header = "This is a bucket list. You can drag items between the lists.",
  add_rank_list(
    text = "Drag from here",
    labels = c("a", "bb", "ccc")
  ),
  add_rank_list(
    text = "to here",
    labels = NULL
  )
),
bookmarkButton())

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

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

Upvotes: 0

Views: 265

Answers (1)

Ash
Ash

Reputation: 1513

The minimal example above is missing a few bits. Below is a minimal example showing bookmarking used to persistently store selections.

library(shiny)

ui <- function(request) {
    
    fluidPage(
        textInput("txt", "Text"),
        checkboxInput("chk", "Checkbox"),
        bookmarkButton()
    )
    
}

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

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

Bucket lists don't appear to be natively supported by bookmarks. You can manually store and retrieve data from bookmarks using onBookmark and onRestore and then renderUI to update your bucket list.


Edit: Additional example showing the use of onBookmark and onRestore and then renderUI to get bucket lists working with bookmarks.

library(shiny)
library(sortable)

ui <- function(request) {
    
    fluidPage(

        uiOutput("bucket"),
        bookmarkButton()
        
    )
    
}

server <- function(input, output, session) { 
    
    output$bucket <- renderUI({
        
        bucket_list(
            header = "This is a bucket list. You can drag items between the lists.",
            add_rank_list(
                text = "Drag from here",
                labels = c("a", "bb", "ccc"),
                input_id = 'list1'
            ),
            add_rank_list(
                text = "to here",
                labels = NULL,
                input_id = 'list2'
            )
        )
        
    })
    
    #When creating a bookmark, manually store the selections from our bucket lists
    onBookmark(function(state) {
        
        state$values$list1 <- input$list1
        state$values$list2 <- input$list2
    })
    
    #When restoring a bookmark, manually retrieve the selections from our bucket lists and re-render the bucket list input
    onRestore(function(state) {
      
        output$bucket <- renderUI({
            
            bucket_list(
                header = "This is a bucket list. You can drag items between the lists.",
                add_rank_list(
                    text = "Drag from here",
                    labels = state$values$list1,
                    input_id = 'list1'
                ),
                add_rank_list(
                    text = "to here",
                    labels = state$values$list2,
                    input_id = 'list2'
                )
            )
            
        })
        
    })
    
}

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

Upvotes: 1

Related Questions