React to menuItem() tab selection

↘锁芯ラ 提交于 2020-01-12 07:30:09

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/37595124/react-to-menuitem-tab-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!