Search code examples
rshinybslib

How to use `bslib::accordion_panel_update()` for updating the panel's title


I would like to dynamically change the title of an accordion panel. I think bslib::accordion_panel_update is the right function to do that, but I cannot get it to work.

Here is a minimal example:

ui <- bslib::page_sidebar(
    sidebar=bslib::sidebar(
        open="always",
        position="right",
        shiny::selectInput(
            inputId="acc_title", 
            label="Accordion Panel's title",
            choices=c("Title 1", "Title 2"),
            selected="Title 1",
            multiple=FALSE
        )
    ),
    bslib::card(
        "Some content",
        bslib::card_footer(
            bslib::accordion(
                id="acc",
                open=FALSE,
                multiple=FALSE,
                bslib::accordion_panel(
                    id="acc_panel", # also tried "value=" here
                    "Title 1",
                    "Accordion's content."
                )
            )
        )
    )
)
server = function(input, output, session) {
    shiny::observeEvent(input$acc_title, {
        bslib::accordion_panel_update( # <-- crucial part
            id="acc", 
            target="acc_panel",
            title = input$acc_title
        )
    })
}
shiny::runApp(shinyApp(ui, server))

Maybe I mix up some arguments to accordion_panel_update. How can I get it work?


Solution

  • I found the problem (and the solution).

    bslib::accordion_panel(
        value="acc_panel", # not "id"
        "Title 1",
        "Accordion's content."
    )
    

    In my original code, I was working with shiny modules and had value=ns("acc_panel"). This does not work. Only the accordion needs the namespace function. I guess that is why the parameter is called value and not id.