How does shiny bookmarking work for a dynamically generated radioButtons-list?

(I postet the exact same question at SO here - I will link vise versa if there is a solution)

I have a complex shiny app where I want to add the bookmarking feature from shiny. Unfortunatelly one key parameter for bookmarking is generated dynamically. It is a list of measurements, which is displayed for a specific day, location and type.

The enableBookmarking feature works for the static lists but not for the dynamic one (radiobuttons-list). I played arround with the onBookmark and onRestore function but I don't get it. Perhaps there is another problem (?).

Edit1:
After reading the onBookmark / onRestore documentation again I do not think it is necessary to use it here. Perhaps the observe{updateRadioButtons}-function overwrites the url-bookmarking-parameters?

Here is a reprex for my problem:

library(dplyr, warn.conflicts = FALSE)
library(lubridate)
library(shiny)

measurements <- tibble::tribble(
  ~epoch, ~location, ~type, ~x, ~y,
  1612008000000, "A", "type1", 1, 1,
  1612072800000, "B", "type2", 2, 2,
  1612101600000, "A", "type2", 3, 3
)


ui <- function(request) {
  fluidPage(
    fluidRow(
      column(
        width = 2,
        dateInput(inputId = "select_date", label = "Select Date", value = (Sys.Date() - lubridate::days(1)), weekstart = 1),
        selectInput(inputId = "select_location", label = "Select Location", choices = c("A", "B"), selected = "B", multiple = FALSE),
        selectInput(inputId = "select_type", label = "Select Type", choices = c("type1", "type2"), selected = "type1", multiple = FALSE)
      ),
      column(
        width = 10,
        radioButtons(
          inputId = "radio_measurements", label = h4("Measurements"),
          choices = "",
          selected = NULL
        ),
        bookmarkButton()
      )
    )
  )
}

server <- function(input, output, session) {
  srv_epoch_start <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT")) * 1000)
  srv_epoch_end <- reactive(as.numeric(lubridate::as_datetime(input$select_date, tz = "GMT") + lubridate::days(1)) * 1000)
  srv_loc_selected <- reactive(as.character(input$select_location))
  srv_type_selected <- reactive(as.character(input$select_type))



  ## List radiobuttons ------------------------------------------------------------------------------------

  list_radioMeasurements <- reactive({
    measurements %>%
      filter(epoch >= srv_epoch_start() & epoch < srv_epoch_end()) %>%
      filter(location == srv_loc_selected()) %>%
      filter(type == srv_type_selected()) %>%
      pull(epoch)
  })

  observe({
    updateRadioButtons(
      session = session, inputId = "radio_measurements",
      choices = list_radioMeasurements(),
      selected = ""
    )
  })
}
# Bookmarking
enableBookmarking(store = "url")
shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents

Created on 2021-02-01 by the reprex package (v1.0.0)

Of course in reality I get the measurments list with data from a DB and there are a lot of observe-Events for different tables, i. e. on the measurements-radiobuttons-list too, but that is obviously not the problem. I would be really glad, if you give me a concrete hint how I can solve this.

I found the answer for my problem. onRestored() is running after everything is restored - so after the dynamic radiobuttons-list is generated.

onRestored(function(state) {
  radio_restore <- state$input$radio_measurements
  updateRadioButtons(session, "radio_measurements", selected = radio_restore)
})

Created on 2021-02-03 by the reprex package (v1.0.0)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.