11

In shinydashboard, one can create menuItem()s, which are tabs in the sidebar. I want to be able to poll which tab is active, by using the standard input$foo syntax.

However, I was not able to do so. I tried by referencing the menuItem()'s tabName or id but that did nothing.

Is there a way to do it?

AF7
  • 3,160
  • 28
  • 63

1 Answers1

9

sidebarMenu have optional id parametr you can use it

sidebarMenu(id="menu1",
      menuItem("PointA_",tabName = "PointA") 
    )

On server side use input$menu1

Full working example, print PointA or PointB ( which tap active)

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
  dashboardHeader(
    title = "Shiny"
  ),

  dashboardSidebar(
    sidebarMenu(id="sbmenu",
      menuItem("PointA_",tabName = "PointA") ,
      menuItem("PointB_",tabName = "PointB") 
    )
  ),

  dashboardBody(
    tabItems(
      tabItem("PointA",h1("a")),
      tabItem("PointB",h1("b"))
    )
  )
)


server <- function(input, output) {
  observe(print(input$sbmenu))
}

shinyApp(ui,server)

Update

Find a bit hack variant to do active+ dropdown + input

with using additional fucntion ( get idea here )

working 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(
    title = "Shiny"
  ),

  dashboardSidebar(
    sidebarMenu(id="sbmenu",
                convertMenuItem(menuItem("PointA_",tabName="PointA", selected=TRUE,

                                          checkboxInput("tc", "Test check", value=FALSE)
                         ),'PointA')        ,
                convertMenuItem(menuItem("PointB_",tabName="PointB",checkboxInput("tc2", "Test check", value=FALSE)
                ),'PointB') 
    )
  ),

  dashboardBody(


    tabItems(
      tabItem("PointA",h1("a")),
      tabItem("PointB",h1("b"))
    )
  )
)


server <- function(input, output) {

  observe({
    print(input$sbmenu)

    })


}

shinyApp(ui,server)

Bonus

I dont know about documectation about children etc.

But if you lok at differences between menuItem and menuItem+additionlal element you can see :

aa=menuItem("PointA_",tabName="PointA", selected=TRUE,

         checkboxInput("tc", "Test check", value=FALSE)
)

aa1=menuItem("PointA_",tabName="PointA", selected=TRUE)

> aa
<li class="treeview">
  <a href="#shiny-tab-PointA">
    <span>PointA_</span>
    <i class="fa fa-angle-left pull-right"></i>
  </a>
  <ul class="treeview-menu">
    <div class="form-group shiny-input-container">
      <div class="checkbox">
        <label>
          <input id="tc" type="checkbox"/>
          <span>Test check</span>
        </label>
      </div>
    </div>
  </ul>
</li>
> aa1
<li>
  <a href="#shiny-tab-PointA" data-toggle="tab" data-value="PointA" data-start-selected="1">
    <span>PointA_</span>
  </a>
</li>

So as you see aa1 have data-toggle="tab" data-value="PointA" and you need it to add to aa

But aa have class="treeview" ( i tried to delete this class in inspect to check what changed) you need delete it .

About children you can see at evirioment view in Rstudio

enter image description here

Batanichek
  • 7,761
  • 31
  • 49
  • Does not work for me. I set `sidebarMenu(id="sbmenu", menuItem(....` and then in the `server` observe `observe(print(input$sbmenu))`, but nothing is printed other than a first `NULL`. – AF7 Jun 02 '16 at 15:19
  • Yes it works! However, if in "PointA" you insert another item, e.g.: `menuItem("PointA_",tabName = "PointA", selected=TRUE, checkboxInput("tc", "Test check", value=FALSE))`... it does not work anymore. – AF7 Jun 03 '16 at 08:08
  • I'm not sure I understand, but in your example when you click on one tab, and select it, it is also printed on the terminal, since `input$sbmenu` changes. This is what I want. However, if you add even a single item inside the tabs (as I wrote before), this does not happen anymore: when you click on a tab (and expand it), `input$sbmenu` does not change anymore. You can easily verify this by just adding `, checkboxInput("tc", "Test check", value=FALSE)` in your example. I need tabs to both contain other items and to report their status in `input$sbmenu`. – AF7 Jun 03 '16 at 09:03
  • thank you, that works nicely! How did you know how to access menuItems, via $children etc.? Is there any documentation? I've not managed to find it. – AF7 Jun 03 '16 at 12:38
  • 1
    Add a bit about children ( but its only ideas and not alway good way) – Batanichek Jun 03 '16 at 12:53
  • 2
    The related doumentation can be found [here](https://rstudio.github.io/shinydashboard/behavior.html#the-small-print-1). Also please see [this](https://stackoverflow.com/a/62834634/9841389) related answer. – ismirsehregal Jul 22 '22 at 09:30