在shinydashboard
中,可以创建menuItem()
,它们是侧边栏中的选项卡。我希望能够使用标准 input$foo
语法轮询哪个选项卡处于事件状态。
但是,我没能做到。我尝试引用 menuItem()
的 tabName
或 id
但没有任何作用。
有办法吗?
最佳答案
sidebarMenu
有可选的 id 参数,您可以使用它
sidebarMenu(id="menu1",
menuItem("PointA_",tabName = "PointA")
)
在服务器端使用input$menu1
完整的工作示例,打印PointA
或PointB
(点击事件)
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
关于对 menuItem() 选项卡选择使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37595124/