1

How can I read parameters of a box element in shinydashboard?

box(..., title = NULL, footer = NULL, status = NULL,
  solidHeader = FALSE, background = NULL, width = 6, height = NULL,
  collapsible = FALSE, collapsed = FALSE)

In particular, I want to save parameter collapsed so that application does not redraw the box in collapsed state if a user expanded it (or vise versa).

Parameter collapsed returns to the originally set value if the tabs are generated.

Example code is below. Collapsed state is reset after adding a tab by the button.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Collapsed Box Example"),
  dashboardSidebar(sidebarMenuOutput("menu"),
                   actionButton("addTab", label = "Add Tab")),
  dashboardBody(uiOutput("body"))
)

server <- function(input, output) {

  tabsCount <- reactiveVal(2)

  #count button clicks
  observeEvent(input$addTab, {
    newValue <- tabsCount() + 1
    tabsCount(newValue)
  }
  )

  output$menu <- renderMenu(sidebarMenu(
    do.call(menuItem, c(text = "Tabs", tabName = "tabs", startExpanded = T,
                        lapply(1:tabsCount(), function(i) {
                          menuSubItem(text = paste0("Tab ", i),
                                      tabName=paste0("tab",i))  
                        })
  ))
  )
  )

  output$body <- renderUI({
    Tabs <- vector("list", tabsCount())
    for(i in 1:tabsCount()) {
      tabname <- paste0("tab",i)
      Tabs[[i]] <- tabItem(tabName = tabname, uiOutput(tabname))
    }
    do.call(tabItems, Tabs)
  })
  observe({
    for (i in 1:tabsCount()) {
      local({
        my_i <- i
        tabname <- paste0("tab", my_i)
        output[[tabname]] <- renderUI(
          box(title = paste("Box", my_i, sep = " "), collapsible = T, collapsed = T)
        )
      })
    }
  })
}

shinyApp(ui, server)
Pavel Khokhlov
  • 160
  • 1
  • 8

0 Answers0