Respond to menuItem () tab selection

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

However, I could not do this. I tried to reference menuItem() tabNameor id, but did nothing.

Is there any way to do this?

+4
source share
1 answer

sidebarMenu have an optional id parameter that you can use

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

Server side use input$menu1

Full working example, print PointAor PointB(which are 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 the option with a little hack for the active + drop down menu + input

fucntion ( )

:

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

docummentation ..

 menuItem menuItem + , :

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>

, , aa1 data-toggle="tab" data-value="PointA", aa

aa class="treeview" ( , , ), .

, Rstudio

enter image description here

+4

All Articles