对 menuItem() 选项卡选择使用react

标签 r shiny shinydashboard

shinydashboard中,可以创建menuItem(),它们是侧边栏中的选项卡。我希望能够使用标准 input$foo 语法轮询哪个选项卡处于事件状态。

但是,我没能做到。我尝试引用 menuItem()tabNameid 但没有任何作用。

有办法吗?

最佳答案

sidebarMenu 有可选的 id 参数,您可以使用它

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

在服务器端使用input$menu1

完整的工作示例,打印PointAPointB(点击事件)

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)

更新

找到一个 hack 变体来执行 active+ dropdown + input

使用附加功能(了解 here )

工作示例:

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)

奖金

我不知道有关 child 等的记录。

但是如果你看看两者之间的差异 您可以看到 menuItem 和 menuItem+additionlal 元素:

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 的 evirioment View 中看到的 child

enter image description here

关于对 menuItem() 选项卡选择使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595124/

相关文章:

r - 从分组单选操作按钮禁用单选单选

r - 删除或隐藏数据点的标签

r - 在 R 中对样本进行分类,并用不同的颜色绘制它们

html - R:检查 url 是否存在,httr:GET() 和 url.exists() 的问题

r - 将一列文本 URL 转换为 Shiny 中的事件超链接

r - 为什么我的缩放图从 ggplot2 空白中 Shiny

r - 了解 order() 函数

mysql - 如何让 Shiny 的应用程序与云关系数据库(在 MySQL 中)对话?

css - Shiny 的 R - 允许滚动条在 div 之上而不是在 div 之内

r - tagAssert 创建一个 Shiny 的仪表板时出错