我创建了以下闪亮的应用程序。该应用包含以下部分
导入必要的礼包
library(shiny)
library(shinyjs)
接下来,我们创建美国。请注意,我们正在使用材质切换功能添加切换侧边栏选项,如下所示:
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
useShinyjs(), navbarPage(materialSwitch(inputId = "toggleSidebar",label = "",
value = FALSE, status = "success")),
# Sidebar layout with input and output definitions ----
sidebarLayout(div( id ="Sidebar",
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30))),
# Main panel for displaying outputs ----
mainPanel( # Output: Histogram ----
plotOutput(outputId = "distPlot")
)))
接下来,我们如下创建服务器
# Define server logic required to draw a histogram ----
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")})
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "Sidebar")
})}
我们现在运行该应用程序。
shinyApp(ui, server)
运行该应用程序时,默认情况下,包含边栏数量的滑块的边栏将被折叠,因为该边栏默认为折叠。可以通过按一下拨动开关进行访问。
是否可以在默认情况下打开侧边栏,然后在单击切换开关时将其折叠起来
谢谢
答案 0 :(得分:1)
您可以使用toggle
的{{1}}参数来确定应显示还是隐藏侧边栏:
condition