How to bookmark values of a data table in R Shiny App?

Hi:

I want to create a bookmark from this table when some cells are selected. The 'Bookmark...' button gives a direction in which the selected cells have disappeared. That is, the bookmark gives the same direction for all selections, and corresponds to the empty table. Can you help me? Thanks.

(https://yosu-yurramendi.shinyapps.io/QuestionBookmark/)

library(shiny)
library(DT)

enableBookmarking(store = "url")

ui <- function(request){fluidPage(
HTML("

(select some cells)

"),
DT::dataTableOutput(outputId = "pattern", width = "25%", height = "auto"),
br(),
bookmarkButton(id = "bookmark")
)}

server <- function(input, output, session) {
nrow <- 3
ncol <- 4
proxy <- dataTableProxy('pattern')
checkboxes <- reactive({
as.data.frame(matrix(rep(NA, nrowncol), nrow = nrow, ncol = ncol,
dimnames = list(paste("m", 1:nrow, sep = ""),
paste("n", 1:ncol, sep = ""))))
})
tableData <- reactiveValues(checkboxes = NULL)
observe({tableData$checkboxes <<- checkboxes()})
#Update the table when clicked
observeEvent(req(input$pattern_cells_selected),
{tableData$checkboxes[input$pattern_cells_selected] <-
ifelse(is.na(tableData$checkboxes[input$pattern_cells_selected]), "
", NA)
replaceData(proxy = proxy, data = tableData$checkboxes)
}) # observeEvent

output$pattern <- DT::renderDataTable({ checkboxes() },
selection = list(mode = "single", target = 'cell'),
options = list(
columnDefs = list(list(className = 'dt-center', targets = "_all")),
dom = "t", ordering = FALSE),
escape = FALSE
) # renderDataTable

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

} # server

shinyApp(ui, server)

Looking at your app, the bookmarking generates quite a complicated bookmark url - like:

https://yosu-yurramendi.shinyapps.io/QuestionBookmark/?_inputs_&pattern_columns_selected=null&pattern_cell_clicked={"row":2,"col":3,"value":null}&pattern_rows_current=[1,2,3]&pattern_rows_all=[1,2,3]&bookmark=1&pattern_rows_selected=null&pattern_search=""&pattern_state={"time":1677948471706,"start":0,"length":10,"order":[],"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true},"columns":[{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}},{"visible":true,"search":{"search":"","smart":true,"regex":false,"caseInsensitive":true}}],"childRows":[]}&pattern_cells_selected=[]

That looks like much more than you really want in your bookmark url...

It might be better to use the setBookmarkExclude, onBookmark and onRestore functions to choose what you want the bookmark to contain - see Shiny - Advanced bookmarking

Alternatively, if you find you are fighting the built-in bookmarking apis, you can also "hack" your own solution using session$clientData$url_search to find passed in url parameters - see articles like r - How do you pass parameters to a shiny app via URL - Stack Overflow for more info.

(I did have a quick play to try to get your app working locally - but couldn't get it to work reliably - probably something I had wrong!)