Is there a way in which we can make the sidebarMenu -> menuItems dynamic?


Is there a way in which we can make the sidebarMenu -> menuItems dynamic?



I'm making a ShinyDashboard program and I have some troubles in finding a way to make a loop in the MenuItems. Specifically, I am looking for something that can replace the following lines:


menuItem(
"Section1",
tabName = "Section1",
startExpanded = T,
menuSubItem("Sub Menu 1", tabName = "tab1"),
menuSubItem("Sub Menu 2", tabName = "tab2")
),

menuItem(
"Section2",
tabName = "Section2",
startExpanded = T,
menuSubItem("Sub Menu 1", tabName = "tab1"),
menuSubItem("Sub Menu 2", tabName = "tab2")
)



Here is what I tried :


lapply(1:2, function(i){
do.call(menuItem, c(text = paste0("Section",i), tabName =paste0("Section",i), startExpanded = T,
lapply(1:2, function(j) {
menuSubItem(text = paste0("sub menu ", j), tabName=paste0("tab",j))
}
)))
})



It is throwing the following error:



Error in : $ operator is invalid for atomic vectors



I went through a post that does something similar with dashboardBody -> tabItems



How to make a function in a for loop or lapply loop in a tabItem dashboard shiny



Thanks for the help in advance.




2 Answers
2



I'm not entirely sure if this is what you're looking for, but the code below dynamically creates those two menu items along with their subitems.


library(shiny)
library(shinydashboard)


ui <- dashboardPage(
dashboardHeader(title = "My Page"),
dashboardSidebar(sidebarMenuOutput("sidebar_menu")),
dashboardBody(
NULL
)
)

server <- shinyServer(function(input, output, session) {
output$sidebar_menu <- renderMenu({
sidebarMenu(id = "tab",

menuItem("Section1", tabName="Section2", startExpanded = T,
lapply(1:2, function(i) {
menuSubItem(paste0("Sub Menu ",i), tabName = paste0("tab",i))
})
),
menuItem("Section2", tabName="Section2", startExpanded = T,
lapply(1:2, function(i) {
menuSubItem(paste0("Sub Menu ",i), tabName = paste0("tab",i))
})
)
)


})

})
runApp(list(ui= ui, server = server))





Hi Adam, Thanks for your response....this code generates SubItems dynamically not the menuItem as it is fixing Section 1 and Section 2. I am looking for something that creates these sections dynamically. Please let me know if my understanding is wrong.
– Vinamra Mathur
yesterday




Found the solution


output$menu <- renderMenu(
{
sidebarMenu(id = "sbm",
lapply(1:10, function(i){
do.call(menuItem, c(text = paste0("Section",i), tabName = paste0("Section",i), startExpanded = T,
lapply(1:2, function(j) {
menuSubItem(text = paste0("sub menu ", j), tabName=paste0("tab",j))
}
)))
}
))
})






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift