input$sidebarItemExpanded does not work with convertMenuItem in R Shiny - r

For convertMenuItem, see reference here: https://stackoverflow.com/a/48212169
When I try to get the name of expanded menuItem, it doesn't work. Here's a standalone example:
library(shiny)
library(shinydashboard)
convertMenuItem <- function(mi,tabName) {
mi$children[[1]]$attribs['data-toggle']="tab"
mi$children[[1]]$attribs['data-value'] = tabName
if(length(mi$attribs$class)>0 && mi$attribs$class=="treeview"){
mi$attribs$class=NULL
}
mi
}
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets"),
convertMenuItem(menuItem("Charts", tabName = "charts", icon = icon("bar-chart-o"), expandedName = "CHARTS",
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2")
), "charts")
),
textOutput("res")
),
dashboardBody(
tabItems(
tabItem("dashboard", "Dashboard tab content"),
tabItem("widgets", "Widgets tab content"),
tabItem("subitem1", "Sub-item 1 tab content"),
tabItem("subitem2", "Sub-item 2 tab content")
)
)
)
server <- function(input, output, session) {
output$res <- renderText({
req(input$sidebarItemExpanded)
paste("Expanded menuItem:", input$sidebarItemExpanded)
print(input$sidebarItemExpanded)
})
}
shinyApp(ui, server)
Is there a way to further modify this function so that Expanded Item functionality is also supported?

The following is a workaround to avoid using convertMenuItem.
Instead of it I'm using a hidden menuItem which is displayed once the childful menuItem "Charts" is expanded.
This way input$sidebarItemExpanded works as expected.
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
id = "sidebarID",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets"),
menuItem("Charts", tabName = "charts", icon = icon("bar-chart-o"), expandedName = "CHARTS",
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2")
),
hidden(menuItem("hiddenCharts", tabName = "hiddenCharts"))
),
textOutput("res")
),
dashboardBody(
useShinyjs(),
tabItems(
tabItem("dashboard", "Dashboard tab content"),
tabItem("widgets", "Widgets tab content"),
tabItem("hiddenCharts", "Charts Tab"),
tabItem("subitem1", "Sub-item 1 tab content"),
tabItem("subitem2", "Sub-item 2 tab content")
)
)
)
server <- function(input, output, session) {
observeEvent(input$sidebarItemExpanded, {
if(input$sidebarItemExpanded == "CHARTS"){
updateTabItems(session, "sidebarID", selected = "hiddenCharts")
}
})
output$res <- renderText({
req(input$sidebarItemExpanded)
paste("Expanded menuItem:", input$sidebarItemExpanded)
print(input$sidebarItemExpanded)
})
}
shinyApp(ui, server)

Related

Move from one to the other tabItem by using actionButton() in shiny dashboard

Im trying to add an actionButton() in the fist tabItem that when pressed will move to the second tabItem named widgets. I cannot understand what am I doing wrong.
## app.R ##
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
sidebarMenu(id="inTabset",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
)
)
server <- function(input, output) {
tabItems(
# First tab content
tabItem(tabName = "dashboard",
"dashboard",
actionButton("nextt","Next")
),
# Second tab content
tabItem(tabName = "widgets",
"widgets"
)
)
observeEvent(input$nextt, {
updateTabItems(session, "inTabset",selected = "dashboard")
})
}
shinyApp(ui, server)
The issue is that you added the tabItems in the server instead of inside dashboardBody. Additionally, to switch from dashboard to widgets you have to do selected="widgets" in updateTabItems and finally your server should have an argument session:
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
sidebarMenu(
id = "inTabset",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "dashboard",
actionButton("nextt", "Next")
),
tabItem(
tabName = "widgets"
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$nextt, {
updateTabItems(session, "inTabset", selected = "widgets")
})
}
shinyApp(ui, server)

Link to a tab from dashboardBody

I am trying to have an action button within the Body of a tab (called "Widgets" in code) link to a different tab (called "data_table" in code). I know how to do this if the tab that I want to connect to, "data_table", is one of the menuItems that appears on the sidebarMenu. However, I do not wish for a link to the "data_table" tab to appear in the sidebar. I am stuck. I would have thought I need an "observeEvent"-type command which links the action button to the "data_table" tab. But I don't know what that is. Advice welcome. The code shows the UI side of things.
ui <- dashboardPage(
dashboardHeader(title = "My query"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")),
tabItem(tabName = "widgets",
h2("Widgets"),
actionButton(inputId="seedata", label = "See data")),
tabItem(tabName = "data_table",
h2("Table with the data"))
)
)
)
server <- function(input, output, session) { }
shinyApp(ui, server)
Perhaps you are looking for something like this.
ui <- dashboardPage(
dashboardHeader(title = "My query"),
dashboardSidebar(
sidebarMenu(# Setting id makes input$tabs give the tabName of currently-selected tab
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")),
tabItem(tabName = "widgets", h2("Widgets"),
fluidRow(
tabBox(id = "tabset1", height = "850px", width=12, title = "My Data",
### The id lets us use input$tabset1 on the server to find the current tab
tabPanel("Table with the data", value="tab1", " ",
actionButton(inputId="seedata", label = "See data"),
uiOutput("dataTable")
),
tabPanel("Display Data Table", value="tab2", " ",
#uiOutput("someoutput")
DT::dataTableOutput("testtable")
)
)
)
))
)
)
server <- function(input, output, session) {
output$dataTable <- renderUI({
tagList(
div(style="display: block; height: 350px; width: 5px;",HTML("<br>")),
actionBttn(inputId="datatable",
label="Data Table",
style = "simple",
color = "success",
size = "md",
block = FALSE,
no_outline = TRUE
))
})
observeEvent(input$datatable, {
updateTabItems(session, "tabs", "widgets")
if (input$datatable == 0){
return()
}else{
## perform other tasks if necessary
output$testtable <- DT::renderDataTable(
mtcars,
class = "display nowrap compact", # style
filter = "top", # location of column filters
options = list( # options
scrollX = TRUE # allow user to scroll wide tables horizontally
)
)
}
})
observeEvent(input$datatable, {
updateTabsetPanel(session, "tabset1",
selected = "tab2")
})
}
shinyApp(ui, server)

shinydashboard: Lost `tabItem` responsiveness when including inputs in `menuItem`

I got a dashboard where the tabItem that shows in the dashboardBody is dependant on the menuItem selected on the dashboardMenu, like this:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(dashboardHeader(title = "This works"),
dashboardSidebar(
sidebarMenu(
menuItem("item 1", tabName = "item1", icon = icon("th-list")),
menuItem("item 2", tabName = "item2", icon = icon("list-alt"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "item1",
tabsetPanel(id = "tabs1",
tabPanel("Tab1", plotOutput("1")),
tabPanel("Tab2", plotOutput("2"))
)),
tabItem(tabName = "item2",
tabsetPanel(id = "tabs2",
tabPanel("Tab3", plotOutput("3")),
tabPanel("Tab4", plotOutput("4"))
)
)
)
)
)
server <- function(input, output) {}
shinyApp(ui, server)
However, as soon as I include an input in menuItem, this response is lost:
ui <- dashboardPage(dashboardHeader(title = "This doesn't work"),
dashboardSidebar(
sidebarMenu(
menuItem("item 1", tabName = "item1", icon = icon("th-list"),
checkboxInput("check", label = "check")),
menuItem("item 2", tabName = "item2", icon = icon("list-alt"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "item1",
tabsetPanel(id = "tabs1",
tabPanel("Tab1", plotOutput("1")),
tabPanel("Tab2", plotOutput("2"))
)),
tabItem(tabName = "item2",
tabsetPanel(id = "tabs2",
tabPanel("Tab3", plotOutput("3")),
tabPanel("Tab4", plotOutput("4"))
)
)
)
)
)
server <- function(input, output) {}
shinyApp(ui, server)
Applying this answer to your example works. Here's the solution:
convertMenuItem <- function(mi,tabName) {
mi$children[[1]]$attribs['data-toggle']="tab"
mi$children[[1]]$attribs['data-value'] = tabName
mi
}
ui <- dashboardPage(dashboardHeader(title = "This works now"),
dashboardSidebar(
sidebarMenu(
convertMenuItem(menuItem("item 1", tabName = "item1", icon = icon("th-list"),
checkboxInput("check", label = "check")), tabName = "item1"),
menuItem("item 2", tabName = "item2", icon = icon("list-alt"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "item1",
tabsetPanel(id = "tabs1",
tabPanel("Tab1", plotOutput("1")),
tabPanel("Tab2", plotOutput("2"))
)),
tabItem(tabName = "item2",
tabsetPanel(id = "tabs2",
tabPanel("Tab3", plotOutput("3")),
tabPanel("Tab4", plotOutput("4"))
)
)
)
)
)
server <- function(input, output) {}
shinyApp(ui, server)

Collapse dashboardSidebar menu after click

For the code below, I would like to have a menu (dashboardSidebar) that collapses when any menuItem but item2 is clicked.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(collapsed = TRUE, sidebarMenu(id = "tabs",
menuItem("item1", tabName = "item1", icon = icon("newspaper")),
menuItem("item2", tabName = "item2", icon = icon("tv"),
menuItem("item2_1", tabName = "item2_1", icon = icon("tasks")),
menuItem("item2_2", tabName = "item2_2", icon = icon("flag-checkered")),
menuItem("item2_3", tabName = "item2_3", icon = icon("user-clock"))))),
dashboardBody())
server <- function(input, output) {}
shinyApp(ui, server)
Thanks
Please check the following:
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(collapsed = TRUE, sidebarMenu(
id = "tabs",
menuItem("item1", tabName = "item1", icon = icon("newspaper")),
menuItem(
"item2",
tabName = "item2",
icon = icon("tv"),
menuItem("item2_1", tabName = "item2_1", icon = icon("tasks")),
menuItem(
"item2_2",
tabName = "item2_2",
icon = icon("flag-checkered")
),
menuItem("item2_3", tabName = "item2_3", icon = icon("user-clock"))
)
)),
dashboardBody(useShinyjs(),
tabItems(
tabItem(tabName = "item1",
h2("item1 tab content")),
tabItem(tabName = "item2_1",
h2("item2_1 tab content")),
tabItem(tabName = "item2_2",
h2("item2_2 tab content")),
tabItem(tabName = "item2_3",
h2("item2_3 tab content"))
))
)
server <- function(input, output, session) {
observeEvent(input$tabs, {
shinyjs::toggleClass(selector = "body", class = "sidebar-collapse")
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
The only drawback is, that selecting the same menuItem twice doesn't collapse the sidebar, due to input$tabs remaining unchanged.

Permanently expanded accordion in shinydashboard

Is there any way to set shinydashboard menu to permanently expanded like on image below:
I know accordion menus behave like this (I mean only one can be expanded at the same time) due to documentation but maybe there is some trick to do this or some alternative to implement in my shiny app?
Here is code:
library(shiny)
library(shinydashboard)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarMenu(
# Setting id makes input$tabs give the tabName of currently-selected tab
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets",
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2"),
startExpanded = TRUE),
menuItem("Charts", icon = icon("bar-chart-o"),
menuSubItem("Sub-item 3", tabName = "subitem3"),
menuSubItem("Sub-item 4", tabName = "subitem4"),
startExpanded = TRUE
)
)
)
body <- dashboardBody(
tabItems(
tabItem("dashboard",
div(p("Dashboard tab content"))
),
tabItem("widgets",
"Widgets tab content"
),
tabItem("subitem1",
"Sub-item 1 tab content"
),
tabItem("subitem2",
"Sub-item 2 tab content"
)
)
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) { }
)
Ok, this is very hacky and there may be a better way to do this, but you can use CSS styling to move the links "below" the other content so they cant be clicked using z-index. Unfortunately it looks like you have to code each menu item by hand, referencing its href. Please see this example:
library(shiny)
library(shinydashboard)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarMenu(
tags$head(tags$style(HTML('
a[href="#shiny-tab-widgets"] {
z-index: -99999;
}
a[href="#"] {
z-index: -99999;
}
'))),
# Setting id makes input$tabs give the tabName of currently-selected tab
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", icon = icon("th"), tabName = "widgets",
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2"),
startExpanded = TRUE),
menuItem("Charts", icon = icon("bar-chart-o"),
menuSubItem("Sub-item 3", tabName = "subitem3"),
menuSubItem("Sub-item 4", tabName = "subitem4"),
startExpanded = TRUE
)
)
)
body <- dashboardBody(
tabItems(
tabItem("dashboard",
div(p("Dashboard tab content"))
),
tabItem("widgets",
"Widgets tab content"
),
tabItem("subitem1",
"Sub-item 1 tab content"
),
tabItem("subitem2",
"Sub-item 2 tab content"
)
)
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) { }
)

Resources