Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React to menuItem() tab selection

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?

like image 245
AF7 Avatar asked Jun 02 '16 14:06

AF7


1 Answers

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

like image 185
Batanichek Avatar answered Oct 17 '22 09:10

Batanichek