在R中使用切换开关时,如何默认打开侧边栏

在R中使用切换开关时,如何默认打开侧边栏,r,shiny,toggle,R,Shiny,Toggle,我已经创建了以下闪亮的应用程序。该应用程序由以下部分组成 引进必要的图书馆 library(shiny) library(shinyjs) 接下来我们创建美国。请注意,我们使用材质切换功能添加了一个切换边栏选项,如下所示 # Define UI for app that draws a histogram ---- ui <- fluidPage( # App title ---- titlePanel("Hello Shiny!"), useShinyjs(), navba

我已经创建了以下闪亮的应用程序。该应用程序由以下部分组成

引进必要的图书馆

 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")
    })}
当应用程序运行时,包含带有箱子数量的滑块的侧栏将折叠,默认情况下侧栏将折叠。按下拨动开关即可访问该按钮

是否可以在默认情况下打开侧栏,然后在单击切换开关时将其折叠


谢谢

您可以使用
切换
s
条件
参数来确定侧边栏是显示还是隐藏:

library(shiny)
library(shinyjs)
library(shinyWidgets)

# Define UI for app that draws a histogram ----
ui <- fluidPage(  
  # App title ----
  titlePanel("Hello Shiny!"),
  useShinyjs(), navbarPage(materialSwitch(inputId = "toggleSidebar",label = "", 
                                          value = TRUE, 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", condition = input$toggleSidebar)
  })
  }

shinyApp(ui, server)
库(闪亮)
图书馆(shinyjs)
图书馆(shinyWidgets)
#为绘制直方图的应用程序定义UI----

哦,不。恐怕这会给出相同的输出。当应用程序启动时,侧边栏应该是可见的,然后当我们使用切换开关时,侧边栏应该折叠。侧边栏在启动时是可见的。请复制上面提供的完整代码,然后重试。整洁。非常感谢。添加的shinywidgets是密钥。您正在使用
materialSwitch
,它是
库(shinywidgets)
的一部分。
library(shiny)
library(shinyjs)
library(shinyWidgets)

# Define UI for app that draws a histogram ----
ui <- fluidPage(  
  # App title ----
  titlePanel("Hello Shiny!"),
  useShinyjs(), navbarPage(materialSwitch(inputId = "toggleSidebar",label = "", 
                                          value = TRUE, 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", condition = input$toggleSidebar)
  })
  }

shinyApp(ui, server)