Dynamic menu selection shinydashboard

I created a dynamic cyber menu in R shinydashboard. Despite the fact that I use selected = TRUE , in this dynamic mode, the menu item associated with the menu will not be selected.

How can I make sure that I have control over what menu contents are displayed when starting in this dynamic mode?

I looked through all related topics. Could not find anything that still works. updateTabItems() does not seem to work.

Any ideas? thanks in advance.

 library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(title = "Dynamic sidebar"), dashboardSidebar( sidebarMenuOutput("menu") ), dashboardBody( tabItems( tabItem(tabName = "m1", p("Menu content 1") ), tabItem(tabName = "m2", p("Menu content 2") ) ) ) ) server <- function(input, output) { output$menu <- renderMenu({ sidebarMenu( menuItem("Menu item1", tabName="m1", icon = icon("calendar")), menuItem("Menu item2", tabName="m2", icon = icon("database"),selected = TRUE) ) }) } shinyApp(ui, server) 

Edit: indentation issue that occurs with Romain anwser hsh

+5
source share
2 answers

Use updateTabItems() effectively. To do this, you need to set the identifier for sidebarMenu and update the corresponding menuItem or menuSubItem .

In your particular case, you should do something like this:

 library(shiny) library(shinydashboard) ui <- dashboardPage( dashboardHeader(title = "Dynamic sidebar"), dashboardSidebar( sidebarMenu(id="tabs", sidebarMenuOutput("menu") ) ), dashboardBody( tabItems( tabItem(tabName = "m1", p("Menu content 1") ), tabItem(tabName = "m2", p("Menu content 2") ) ) ) ) server <- function(input, output,session) { output$menu <- renderMenu({ sidebarMenu( menuItem("Menu item1", tabName="m1", icon = icon("calendar")), menuItem("Menu item2", tabName="m2", icon = icon("database")) ) }) isolate({updateTabItems(session, "tabs", "m2")}) } shinyApp(ui, server) 

Edited version to remove indentation issues

+4
source

Why not use an observer that is called only once in app init

 observe({ # called only once at app init updateTabItems(session, "tabs", "m2") }) 
0
source

Source: https://habr.com/ru/post/1247517/


All Articles