1
votes

I am using a shiny::conditionalPanel inside a shinyBS::bsCollapsePanel. I have logic in my app that depends on which shinyBS panel is active (i.e., expanded). This works fine until I activate the conditional panel. If I show the Shiny conditional panel, then the shinyBS collapse panel gets stuck as active even when the panel is inactive (i.e., closed).

How can I modify my code so that the collapsible panels only register as active if they are expanded?

In this example there is a text output indicating the active panel. Switching between panels works correctly unless the conditional panels are shown.

EDIT: It appears this bug may already be documented (https://github.com/ebailey78/shinyBS/issues/38) and there is a possible solution (https://github.com/ebailey78/shinyBS/pull/68/commits).

library(shiny)
library(shinyBS)

# Define UI logic
ui <- fluidPage(

    htmlOutput("activePanel"),

    shinyBS::bsCollapse(
        id = "bsPanels",
        shinyBS::bsCollapsePanel(
            "Panel A",
            value = "panelA",
            checkboxInput("showPanelA",
                          "Show panel",
                          value = FALSE),
            conditionalPanel(
                condition = "input.showPanelA",
                helpText("Panel A conditional content")
            ),
            helpText("Panel A main content")
        ),
        shinyBS::bsCollapsePanel(
            "Panel B",
            value = "panelB",
            checkboxInput("showPanelB",
                          "Show panel",
                          value = FALSE),
            conditionalPanel(
                condition = "input.showPanelB",
                helpText("Panel B conditional content")
            ),
            helpText("Panel B main content")
        )
    )
)

# Define server logic 
server <- function(input, output) {

    output$activePanel <- renderText({
        paste("<b>Active Panel:</b>", paste(input$bsPanels, collapse = ", "))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
1
I had a go at solving by using updateCheckboxInput() in an observeEvent() where the checkbox gets unticked when the panels change...didn't work with the bug though, glad you've found a possible solution!Jaccar

1 Answers

0
votes

There is some discussion of this issue on the shinyBS project page (https://github.com/ebailey78/shinyBS/issues/38). However, I had limited success with the proposed solutions.

The best solution I have found is to use shinyjs::showElement and shinyjs::hideElement.

library(shiny)
library(shinyBS)
library(shinyjs)

# Define UI logic
ui <- fluidPage(

    useShinyjs(),

    htmlOutput("activePanel"),

    shinyBS::bsCollapse(
        id = "bsPanels",
        shinyBS::bsCollapsePanel(
            "Panel A",
            value = "panelA",
            checkboxInput("showPanelA",
                          "Show panel",
                          value = FALSE),
            uiOutput("condPanelA"),
            helpText("Panel A main content")
        ),
        shinyBS::bsCollapsePanel(
            "Panel B",
            value = "panelB",
            checkboxInput("showPanelB",
                          "Show panel",
                          value = FALSE),
            uiOutput("condPanelB"),
            helpText("Panel B main content")
        )
    )
)

# Define server logic 
server <- function(input, output) {

    output$activePanel <- renderText({
        paste("<b>Active Panel:</b>", paste(input$bsPanels, collapse = ", "))
    })

    # Logic for conditional panels
    output$condPanelA <- renderUI({
      helpText("Panel A conditional content")
    })
    observe({
      if(input$showPanelA) {
        show("condPanelA")
      } else {
        hide("condPanelA")
      }
    })
    output$condPanelB <- renderUI({
      helpText("Panel B conditional content")
    })
    observe({
      if(input$showPanelB) {
        show("condPanelB")
      } else {
        hide("condPanelB")
      }
    })

}

# Run the application 
shinyApp(ui = ui, server = server)