我试图使用shinydashboard设置一个闪亮的应用程序,并且在大多数情况下,祝你好运。但是,我遇到了一种我认为可以避免的侧边栏行为的怪癖,但还没有找到。
以下是一个重现我遇到的问题的小例子。基本上,有两个sidebarMenus - Menu One和Menu Two,每个都有两个menuSubItems。在菜单项中切换子项可以正常工作。所以,如果我想从subItemOne切换到subItemTwo,没有问题。我可以整天这样做。
我还可以跨菜单切换到subItems,这样从subItemOne跳转到subItemThree,这很好。问题在于试图切换回来。如果选择了subItemOne,并且我尝试转到subItemThree并将返回转到subItemOne,我就无法做到。我必须转到subItemTwo,然后我可以打开SubItemOne。
有没有办法纠正这种设置,以便我可以直接从subItemOne跳转到subItemThree(或者两个和四个)然后再回来?
library('shiny')
library('shinydashboard')
# Sidebar #############################
sidebar <- dashboardSidebar(
width = 290,
sidebarMenu(
menuItem('Menu One', tabName = 'menuOne', icon = icon('line-chart'),
collapsible =
menuSubItem('Sub-Item One', tabName = 'subItemOne'),
menuSubItem('Sub-Item Two', tabName = 'subItemTwo')
)
),
sidebarMenu(
menuItem('Menu Two', tabName = 'menuTwo', icon = icon('users'),
collapsible =
menuSubItem('Sub-Item Three', tabName = 'subItemThree'),
menuSubItem('Sub-Item Four', tabName = 'subItemFour')
)
)
)
# Body #############################
body <- dashboardBody(
tabItems(
tabItem(tabName = 'subItemOne',
h2('Selected Sub-Item One')
),
tabItem(tabName = 'subItemTwo',
h2('Selected Sub-Item Two')
),
tabItem(tabName = 'subItemThree',
h2('Selected Sub-Item Three')
),
tabItem(tabName = 'subItemFour',
h2('Selected Sub-Item Four')
)
)
)
# UI #############################
ui <- dashboardPage(
dashboardHeader(title = 'Test', titleWidth = 290),
sidebar,
body
)
# Server #############################
server <- function(input, output){
}
shinyApp(ui, server)
答案 0 :(得分:5)
问题是选项卡项保持活动状态,单击活动选项卡项不会更新UI。这可以通过一些Javascript修复。
library('shiny')
library('shinydashboard')
# Sidebar #############################
sidebar <- dashboardSidebar(
tags$head(
tags$script(
HTML(
"
$(document).ready(function(){
// Bind classes to menu items, easiet to fill in manually
var ids = ['subItemOne','subItemTwo','subItemThree','subItemFour'];
for(i=0; i<ids.length; i++){
$('a[data-value='+ids[i]+']').addClass('my_subitem_class');
}
// Register click handeler
$('.my_subitem_class').on('click',function(){
// Unactive menuSubItems
$('.my_subitem_class').parent().removeClass('active');
})
})
"
)
)
),
width = 290,
sidebarMenu(
menuItem('Menu One', tabName = 'menuOne', icon = icon('line-chart'),
collapsible =
menuSubItem('Sub-Item One', tabName = 'subItemOne'),
menuSubItem('Sub-Item Two', tabName = 'subItemTwo')
)
),
sidebarMenu(
menuItem('Menu Two', tabName = 'menuTwo', icon = icon('users'),
collapsible =
menuSubItem('Sub-Item Three', tabName = 'subItemThree'),
menuSubItem('Sub-Item Four', tabName = 'subItemFour')
)
)
)
# Body #############################
body <- dashboardBody(
tabItems(
tabItem(tabName = 'subItemOne',
h2('Selected Sub-Item One')
),
tabItem(tabName = 'subItemTwo',
h2('Selected Sub-Item Two')
),
tabItem(tabName = 'subItemThree',
h2('Selected Sub-Item Three')
),
tabItem(tabName = 'subItemFour',
h2('Selected Sub-Item Four')
)
)
)
# UI #############################
ui <- dashboardPage(
dashboardHeader(title = 'Test', titleWidth = 290),
sidebar,
body
)
# Server #############################
server <- function(input, output){
}
shinyApp(ui, server)