How to see if a shiny dashboard box is collapsed from the server side

Viewed 1700

I'm trying to find a way to check whether a Shiny Dashboard Box is collapsed or expanded.

By reading the great reply by @daattali in How to manually collapse a box in shiny dashboard I know it is possible to collapse the box from the server side by using the shinyjs package, as illustrated in the code below

library(shiny)
library(shinydashboard)
library(shinyjs)

jscode <- "
shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    extendShinyjs(text = jscode),
    actionButton("bt1", "Collapse box1"),
    actionButton("bt2", "Collapse box2"),
    br(), br(),
    box(id = "box1", collapsible = TRUE, p("Box 1")),
    box(id = "box2", collapsible = TRUE, p("Box 2"))
  )
)

server <- function(input, output) {
  observeEvent(input$bt1, {
    js$collapse("box1")
  })
  observeEvent(input$bt2, {
    js$collapse("box2")
  })
}

shinyApp(ui, server)  

By inspecting the UI HTML I see that answer to my problem could be solved by accessing icon class (to see whether it is fa fa-plus or fa fa-minus), but I have no idea how to do that.

Any help would be greatly appreciated.

Cheers

1 Answers
Related