{shinydashboard}
skeleton elements are :
dashboardPage()
(page wrapper)dashboardHeader()
(navbar)dashboardSidebar()
(left sidebar)However, AdminLTE has a footer and a right sidebar, also known as controlbar. The footer is usually a good place to put contact information like mail, authors and copyrights, while the controlbar may contain secondary inputs or extra options that are not necessary to be shown in the app.
To include the controlbar, use dashboardControlbar()
in
the dedicated controlbar parameter. It has several options:
updateControlbar()
. This is useful if the controlbar would
have to open as a result of another action, to indicate users they have
to play with itdashboardPagge()
option parameter!The app below will show an open controlbar at start.
As mentioned above, the most powerful feature is the possibility to control elements on the server. In the example below, the main sidebar has 3 items, each item will open a specific menu item in the controlbar.
We first create 3 generic sidebar menu items using
lapply
. Note that the controlbar menu is defined above in
the previous example.
sidebarMenu(
id = "sidebarMenu",
lapply(1:3, function(i) {
menuItem(
sprintf("Menu %s", i),
tabName = sprintf("menu_%s", i),
icon = icon("circle")
)
})
)
input$sidebarMenu
takes values in menu_1
,
menu_2
and menu_3
. On the server side, we only
recover the item index by splitting the input value as follows
strsplit(input$sidebarMenu, "_")[[1]][2]
. Then we may
conditionally open the controlbar depending on the index value. The
update controlbar menu function will update the controlbar menu item
according to the index value, that is
updateControlbarMenu("controlbarMenu", selected = idx)
.
To include even more interactivity, we listen to
input$controlbarMenu
. When the second item is clicked, we
toggle the box sidebar with
updateBoxSidebar("boxSidebar")
.
In conclusion, you may imagine a lot of other situations.
shinyApp(
ui = dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(
minified = TRUE,
collapsed = TRUE,
sidebarMenu(
id = "sidebarMenu",
lapply(1:3, function(i) {
menuItem(
sprintf("Menu %s", i),
tabName = sprintf("menu_%s", i),
icon = icon("circle")
)
})
)
),
body = dashboardBody(
tabItems(
tabItem(tabName = "menu_1", "Content 1"),
tabItem(
tabName = "menu_2",
box(
title = "Always the same plot!",
collapsible = TRUE,
plotOutput("distPlot"),
sidebar = boxSidebar(
id = "boxSidebar",
background = "#808080",
width = "50%",
sliderInput(
"obs",
"Number of observations:",
min = 0,
max = 1000,
value = 500
)
)
)
)
)
),
controlbar = dashboardControlbar(
id = "controlbar",
menu
),
title = "DashboardPage"
),
server = function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs))
})
# Switch controlbar menu based on sidebar item value. Moreover
# if the sidebar menu item is 2, the controlbar opens
observeEvent(input$sidebarMenu, {
idx <- strsplit(input$sidebarMenu, "_")[[1]][2]
if (idx == 2) {
updateControlbar("controlbar")
}
updateControlbarMenu("controlbarMenu", selected = idx)
})
# Clicking on the second controlbar item makes the box sidebar open
observeEvent(input$controlbarMenu, {
if (input$controlbarMenu == "Tab 2") updateBoxSidebar("boxSidebar")
})
observeEvent(input$num, {
updateSliderInput(session, "obs", value = input$num)
}, ignoreInit = TRUE)
}
)