Shinydashboard中特定于选项卡的侧边栏 [英] tab specific sidebar in shinydashboard
本文介绍了Shinydashboard中特定于选项卡的侧边栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
shinyjs
包来允许onclick
类型的事件在选项卡集的选项卡之间导航。每个选项卡都有一个特定的侧边栏,在每个选项卡之间有多种(两种)方式(即,通过单击选项卡本身或通过单击ValueBox)。我希望确保无论您以何种方式访问特定的选项卡,都会加载正确的侧边栏。
# load libraries
require(shiny)
require(shinydashboard)
require(shinyjs)
# create a simple app
ui <- dashboardPage(
title='Loading graphs',
dashboardHeader(
title = 'Loading Graphs'
),
dashboardSidebar(
div(id='tab1_sidebar',
sliderInput('tab1_slider', 'tab1 slider', min=2,max=7,value=2)),
shinyjs::hidden(
div(id='tab2_sidebar',
sliderInput('tab2_slider', 'tab2 slider', min=2,max=7,value=2))
)
),
dashboardBody(
useShinyjs(),
tabsetPanel(
id = "navbar",
tabPanel(title="tab1 title",id="tab1",value='tab1_val',
valueBoxOutput('tab1_valuebox')),
tabPanel(title="tab2 title",id="tab2",value='tab2_val',
valueBoxOutput('tab2_valuebox'))
)
)
)
server <- shinyServer(function(input, output, session) {
output$tab1_valuebox <- renderValueBox({
valueBox('1000',subtitle = "blah blah",icon = icon("car"),
color = "blue"
)
})
output$tab2_valuebox <- renderValueBox({
valueBox('2000',subtitle = "blah2 blah2",icon = icon("car"),
color = "red"
)
})
# on click of a tab1 valuebox
shinyjs::onclick('tab1_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab2_val')
# update the sidebar to match the tab
toggle('tab1_sidebar')
toggle('tab2_sidebar')
})
# on click of a tab2 valuebox
shinyjs::onclick('tab2_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab1_val')
# update the sidebar to match the tab
toggle('tab1_sidebar')
toggle('tab2_sidebar')
})
})
shinyApp(ui=ui,server=server)
在上面的代码中,当单击选项卡时,侧边栏不会更改,但如果我将以下代码包含到服务器组件中以允许单击选项卡,它似乎无法正确调整...
# change sidebar based on navbar value....
observeEvent(input$navbar,{
if(input$navbar=='tab1_val'){
toggle('tab1_sidebar')
toggle('tab2_sidebar')
} else if(input$navbar=='tab2_val'){
toggle('tab1_sidebar')
toggle('tab2_sidebar')
}
})
如有任何帮助,我们将不胜感激...
推荐答案
目前,您的代码中似乎没有任何逻辑告诉滑块在切换选项卡时更新,只在单击ValueBox时更新。
您可以使用不同的方法来解决此问题。
选项1:在导航栏更改时侦听并使用toggle()
和condition
toggle()
函数。当选项卡值更改时(通过单击valueBox或通过单击选项卡),然后调用toggle()
函数。很小但很重要的区别。
# load libraries
require(shiny)
require(shinydashboard)
require(shinyjs)
# create a simple app
ui <- dashboardPage(
title='Loading graphs',
dashboardHeader(
title = 'Loading Graphs'
),
dashboardSidebar(
div(id='tab1_sidebar',
sliderInput('tab1_slider', 'tab1 slider', min=2,max=7,value=2)),
shinyjs::hidden(
div(id='tab2_sidebar',
sliderInput('tab2_slider', 'tab2 slider', min=2,max=7,value=2))
)
),
dashboardBody(
useShinyjs(),
tabsetPanel(
id = "navbar",
tabPanel(title="tab1 title",id="tab1",value='tab1_val',
valueBoxOutput('tab1_valuebox')),
tabPanel(title="tab2 title",id="tab2",value='tab2_val',
valueBoxOutput('tab2_valuebox'))
)
)
)
server <- shinyServer(function(input, output, session) {
values <- reactiveValues(selectedTab = 1)
observeEvent(input$navbar, {
toggle("tab1_sidebar", condition = input$navbar == "tab1_val")
toggle("tab2_sidebar", condition = input$navbar == "tab2_val")
})
output$tab1_valuebox <- renderValueBox({
valueBox('1000',subtitle = "blah blah",icon = icon("car"),
color = "blue"
)
})
output$tab2_valuebox <- renderValueBox({
valueBox('2000',subtitle = "blah2 blah2",icon = icon("car"),
color = "red"
)
})
# on click of a tab1 valuebox
shinyjs::onclick('tab1_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab2_val')
})
# on click of a tab2 valuebox
shinyjs::onclick('tab2_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab1_val')
})
})
shinyApp(ui=ui,server=server)
选项2:使用conditionalPanel()
作为shinyjs
的作者,我喜欢toggle
函数,但在这种情况下它不是绝对必要的,您只需使用conditionalPanel()
即可。以下是我认为您需要的代码:
# load libraries
require(shiny)
require(shinydashboard)
require(shinyjs)
# create a simple app
ui <- dashboardPage(
title='Loading graphs',
dashboardHeader(
title = 'Loading Graphs'
),
dashboardSidebar(
conditionalPanel("input.navbar == 'tab1_val'",
div(id='tab1_sidebar',
sliderInput('tab1_slider', 'tab1 slider', min=2,max=7,value=2))
),
conditionalPanel("input.navbar == 'tab2_val'",
div(id='tab2_sidebar',
sliderInput('tab2_slider', 'tab2 slider', min=2,max=7,value=2))
)
),
dashboardBody(
useShinyjs(),
tabsetPanel(
id = "navbar",
tabPanel(title="tab1 title",id="tab1",value='tab1_val',
valueBoxOutput('tab1_valuebox')),
tabPanel(title="tab2 title",id="tab2",value='tab2_val',
valueBoxOutput('tab2_valuebox'))
)
)
)
server <- shinyServer(function(input, output, session) {
output$tab1_valuebox <- renderValueBox({
valueBox('1000',subtitle = "blah blah",icon = icon("car"),
color = "blue"
)
})
output$tab2_valuebox <- renderValueBox({
valueBox('2000',subtitle = "blah2 blah2",icon = icon("car"),
color = "red"
)
})
# on click of a tab1 valuebox
shinyjs::onclick('tab1_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab2_val')
})
# on click of a tab2 valuebox
shinyjs::onclick('tab2_valuebox',expr={
# move to tab2
updateTabsetPanel(session, "navbar", 'tab1_val')
})
})
shinyApp(ui=ui,server=server)
这篇关于Shinydashboard中特定于选项卡的侧边栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文