ebailey78 / shinyBS

Twitter Bootstrap Components for Shiny
182 stars 47 forks source link

updateCollapse() doesn't work correctly without attaching shinyBS #92

Open matdoering opened 7 years ago

matdoering commented 7 years ago

The documentation of shinyBS states that shinyBS should be attached after loading shiny in server.R/ui.R. However, for packages providing a shinyApp utilizing shinyBS it would be beneficial if attaching shinyBS wasn't a requirement. Indeed, when shinyBS it not attached, it seems that everything works fine except for updating bsCollapes.

Here's an example based on bsExample("Collapses"). Note that the collapses don't change when shinyBS is not loaded via library().

ui.R

#library(shiny)
#library(shinyBS)
shiny::fluidPage(
   shiny::sidebarLayout(
     shiny::sidebarPanel(shiny::HTML("This button will open Panel 1 using <code>updateCollapse</code>."),
                  shiny::actionButton("p1Button", "Push Me!"),
                  shiny::selectInput("styleSelect", "Select style for Panel 1",
                   c("default", "primary", "danger", "warning", "info", "success"))
     ),
     shiny::mainPanel(
       shinyBS::bsCollapse(id = "collapseExample", open = "Panel 2",
                  shinyBS::bsCollapsePanel("Panel 1", "This is a panel with just text ",
                   "and has the default style. You can change the style in ",
                   "the sidebar.", style = "info"),
                  shinyBS::bsCollapsePanel("Panel 2", "This panel has a generic plot. ",
                   "and a 'success' style.", shiny::plotOutput("genericPlot"), style = "success")
       )
     )
   )
) 

server.R

shiny::shinyServer(
 function(input, output, session) {
   output$genericPlot <- shiny::renderPlot(plot(rnorm(100)))
   shiny::observeEvent(input$p1Button, ({
     shinyBS::updateCollapse(session, "collapseExample", open = "Panel 1")
   }))
   shiny::observeEvent(input$styleSelect, ({
     shinyBS::updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
   }))
 }
)
GregorDeCillia commented 7 years ago

I actually had similar issues and found a fix here. It seems the shinyBS resources are included with .onAttach rather than .onLoad (see source file).

My fix was to add the following lines somewhere in global.R or outside the app.

shiny::addResourcePath("sbs", system.file("www", package="shinyBS"))