我有一个闪亮的应用程序,在navbarPage中的tabPanel内有一个mainPanel和一个sidebarPanel。我需要一个隐藏sidebarPanel的选项,类似于:Hide sidebar in default in shinydashboard和https://github.com/daattali/shinyjs/issues/43。
actionButton应该控制sidebarPanel是显示还是折叠。
这是代码:
library(shiny)
library(shinyjs)
ui <- fluidPage(
navbarPage("",
tabPanel("tab",
sidebarPanel(
useShinyjs()
),
mainPanel(actionButton("showSidebar", "Show sidebar"),
actionButton("hideSidebar", "Hide sidebar")
)
)
)
)
server <-function(input, output, session) {
observeEvent(input$showSidebar, {
shinyjs::removeClass(selector = "body", class = "sidebarPanel-collapse")
})
observeEvent(input$hideSidebar, {
shinyjs::addClass(selector = "body", class = "sidebarPanel-collapse")
})
}
shinyApp(ui, server)
希望有人可以提供帮助:)
答案 0 :(得分:8)
我修改了您的代码以隐藏并显示侧边栏。要为id
创建sidebarPanel
,我已将其包含在div中并为其指定id = Sidebar
。要显示和隐藏侧边栏,我使用了shinyjs
函数show
和hide
,其ID为Sidebar
。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage("",
tabPanel("tab",
div( id ="Sidebar",sidebarPanel(
)),
mainPanel(actionButton("showSidebar", "Show sidebar"),
actionButton("hideSidebar", "Hide sidebar")
)
)
)
)
server <-function(input, output, session) {
observeEvent(input$showSidebar, {
shinyjs::show(id = "Sidebar")
})
observeEvent(input$hideSidebar, {
shinyjs::hide(id = "Sidebar")
})
}
shinyApp(ui, server)
希望它有所帮助!
答案 1 :(得分:1)
我用导航栏和多个选项卡上的切换按钮做了一个例子。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage(title = tagList("title",actionLink("sidebar_button","",icon = icon("bars"))),
id = "navbarID",
tabPanel("tab1",
div(class="sidebar"
,sidebarPanel("sidebar1")
),
mainPanel(
"MainPanel1"
)
),
tabPanel("tab2",
div(class="sidebar"
,sidebarPanel("sidebar2")
),
mainPanel(
"MainPanel2"
)
)
)
)
server <-function(input, output, session) {
observeEvent(input$sidebar_button,{
shinyjs::toggle(selector = ".sidebar")
})
}
shinyApp(ui, server)
========================================
我创建了一个不使用 sidepanel 类的更简单的示例,但我不确定它是否适用于所有环境。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage(title = tagList("title",actionLink("sidebar_button","",icon = icon("bars"))),
tabPanel("tab1",
sidebarPanel("sidebar1"),
mainPanel("MainPanel1")
),
tabPanel("tab2",
sidebarPanel("sidebar2"),
mainPanel("MainPanel2")
)
)
)
server <-function(input, output, session) {
observeEvent(input$sidebar_button,{
shinyjs::toggle(selector = ".tab-pane.active div:has(> [role='complementary'])")
})
}
shinyApp(ui, server)
========================================
我终于完成了我的可折叠侧边栏面板,并且 Shinyjs runjs 允许您在侧栏面板折叠时扩展主面板的宽度。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage(title = tagList("title",actionLink("sidebar_button","",icon = icon("bars"))),
tabPanel("tab1",
sidebarPanel("sidebar1",width=3),
mainPanel("MainPanel1",width=9,style="background-color:gray")
),
tabPanel("tab2",
sidebarPanel("sidebar2",width=5)
,mainPanel("MainPanel2",width=7,style="background-color:gray")
)
)
)
server <-function(input, output, session) {
observeEvent(input$sidebar_button,{
shinyjs::toggle(selector = ".tab-pane.active div:has(> [role='complementary'])")
js_maintab <- paste0('$(".tab-pane.active div[role=',"'main'",']")')
runjs(paste0('
width_percent = parseFloat(',js_maintab,'.css("width")) / parseFloat(',js_maintab,'.parent().css("width"));
if (width_percent == 1){
',js_maintab,'.css("width","");
} else {
',js_maintab,'.css("width","100%");
}
'))
})
}
shinyApp(ui, server)
答案 2 :(得分:0)
以前评论中建议的切换选项示例。
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage("",
tabPanel("tab",
div( id ="Sidebar",sidebarPanel(
)),
mainPanel(actionButton("toggleSidebar", "Toggle sidebar")
)
)
)
)
server <-function(input, output, session) {
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "Sidebar")
})
}
shinyApp(ui, server)