R shinydashboard dynamic menu selection
<p>I have created dynamic sibebar menus in R shinydashboard. Even though I use <code>selected = TRUE</code>, no menuItem associated with a menu gets selected at startup in this dynamic mode.</p>
<p>How can I make sure I have control on which menuItem's contents is shown at startup in this dynamic mode?</p>
<p>I have been searching all over through similar posts. Could not find anything that work so far. <code>updateTabItems()</code> did not seem to work.</p>
<p>Any ideas?
thanks from advance.</p>
<pre><code>library(shiny)
library(shinydashboard)
ui &lt;- dashboardPage(
  dashboardHeader(title = &quot;Dynamic sidebar&quot;),
  dashboardSidebar(
    sidebarMenuOutput(&quot;menu&quot;)
  ),
  dashboardBody(
        tabItems(
          tabItem(tabName = &quot;m1&quot;, p(&quot;Menu content 1&quot;) ),
          tabItem(tabName = &quot;m2&quot;, p(&quot;Menu content 2&quot;) )
    )
   )
)
server &lt;- function(input, output) {
  output$menu &lt;- renderMenu({
    sidebarMenu(
      menuItem(&quot;Menu item1&quot;, tabName=&quot;m1&quot;, icon = icon(&quot;calendar&quot;)),
      menuItem(&quot;Menu item2&quot;, tabName=&quot;m2&quot;, icon = icon(&quot;database&quot;),selected = TRUE)
    )
  })
}
shinyApp(ui, server)
</code></pre>
<p>Edit : Indentation problem that occurs with Romain's anwser
<a href="https://i.stack.imgur.com/mgoBi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mgoBi.png" alt="hsh" /></a></p>