bslib icon indicating copy to clipboard operation
bslib copied to clipboard

shiny bookmarking problem when using accordion that is assigned to a variable

Open benubah opened this issue 2 years ago • 3 comments

The problem

Hi, while testing shiny bookmarking using bslib, I encountered the following issues:

  1. The bookmarking appears to be problematic using bslib when my selectizeInput is inside an accordion - the state of the inputs are not restored. But outside an accordion, the state of the selectizeInput is restored using the url.
  2. I cannot restore the state of the app to the nav_panel of my choice (which is Two in the reprex below). The app always restores to the first nav_panel, One.
library(shiny)
library(bslib)

accordion_filters <- bslib::accordion(
  accordion_panel(
    "Choose filters", icon = bsicons::bs_icon("sliders"),
    selectizeInput(
      inputId = "a_filters",
      label = "",
      choices = c("A","B","C","D"),
      selected = c("A"),
      multiple = TRUE,
      width = "100%",
      size = 15,
      options = list(plugins= list('remove_button'))
    )
  ),
  accordion_panel(
    "Filters", icon = bsicons::bs_icon("funnel-fill"),
    selectizeInput(
      inputId = "x_filters",
      label = "",
      choices = c("X","Y","Z"),
      selected = c("X"),
      multiple = TRUE,
      width = "100%",
      size = 15,
      options = list(plugins= list('remove_button'))
    )
  ),
  bookmarkButton(),
  open = "Filters"
)

ui <- function(request) {
 page_navbar(
  title = "My App",
  sidebar = accordion_filters,
  bg = "#0062cc",
  nav_panel(title = "One"),
  nav_panel(title = "Two", uiOutput("mycards"))
 )
}
server <- function(input, output, session) {
  output$mycards <- renderUI({
    layout_columns(
      col_widths = 6,
   !!!lapply(1:6, function(a) {
      chart_name <- paste0("chart_", a)
      output[[chart_name]] <- renderPlot({
        mtcars |>
          head() |>
          plot()
      })
            bslib::card(
              full_screen = TRUE,
              card_header(class = "bg-dark", chart_name),
              card_body(
                min_height = 500,
                plotOutput(chart_name)
              )
            )
       })
    )
  })
}
shinyApp(ui, server, enableBookmarking = "url")

Session Info


Place your devtools::session_info() here

benubah avatar Dec 18 '23 11:12 benubah

  1. I cannot restore the state of the app to the nav_panel of my choice (which is Two in the reprex below). The app always restores to the first nav_panel, One.

I have used a navset_bar in combination with page_fluid instead of page_navbar to solve this since page_navbar does not have an id argument.

benubah avatar Dec 20 '23 13:12 benubah

Thanks for the report and the reprex @benubah! I'll open a new issue for the page_navbar() bookmarking problem.

I can reproduce the issues with bookmarking inputs inside accordions. Here's a slightly smaller and more minimal reprex:

library(shiny)
library(bslib)

accordion_filters <- accordion(
  accordion_panel(
    "Filters",
    selectInput(
      inputId = "first_choice",
      label = "First choice",
      choices = c("A","B","C","D")
    ),
    textInput("reason", "I like this choice because...")
  )
)

ui <- function(request) {
 page_sidebar(
  title = "My App",
  sidebar = sidebar(
    open = "always",
    accordion_filters,
    selectInput("second_choice", "Second choice", rev(LETTERS)[1:4]),
    bookmarkButton()
  ),
  uiOutput("your_choice")
 )
}

server <- function(input, output, session) {
  output$your_choice <- renderUI({
    markdown(
      sprintf(
        "Your first choice was `%s` and your second choice is `%s`. You chose `%s` because %s.",
        input$first_choice,
        input$second_choice,
        input$first_choice,
        input$reason
      )
    )
  })
}

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

Change all of the inputs and use the bookmark button to create a new link. When you load the bookmarked app, the inputs outside of the accordion are updated to the bookmarked state as expected, but the inputs inside the accordion panel are not.

gadenbuie avatar Dec 20 '23 15:12 gadenbuie

thanks @gadenbuie !

The problem is not from the accordion because I have now realized that I can pass the accordion code straight into the sidebar and it works. Please see reprex below. But storing the accordion in a variable and then passing that variable (accordion_filters) to the sidebar is what does not work.

library(shiny)
library(bslib)


ui <- function(request) {
  page_sidebar(
    title = "My App",
    sidebar = sidebar(
      open = "always",
      accordion(
        accordion_panel(
          "Filters",
          selectInput(
            inputId = "first_choice",
            label = "First choice",
            choices = c("A","B","C","D")
          ),
          textInput("reason", "I like this choice because...")
        )
      ),
      selectInput("second_choice", "Second choice", rev(LETTERS)[1:4]),
      bookmarkButton()
    ),
    uiOutput("your_choice")
  )
}

server <- function(input, output, session) {
  output$your_choice <- renderUI({
    markdown(
      sprintf(
        "Your first choice was `%s` and your second choice is `%s`. You chose `%s` because %s.",
        input$first_choice,
        input$second_choice,
        input$first_choice,
        input$reason
      )
    )
  })
}

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

benubah avatar Dec 20 '23 18:12 benubah