【问题标题】:How to have the sidebar open by default when using a toggle switch in R在R中使用切换开关时如何默认打开侧边栏
【发布时间】:2019-09-23 15:28:33
【问题描述】:

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

导入必要的库

 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)

当应用程序运行时,包含带有 bin 数量的滑块的侧边栏会被折叠,因为侧边栏默认是折叠的。按下拨动开关也可以访问相同的内容。

是否可以默认打开侧边栏,然后单击切换开关时折叠相同

谢谢

【问题讨论】:

    标签: r shiny toggle


    【解决方案1】:

    您可以使用toggles condition 参数来确定侧边栏是显示还是隐藏:

    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)
    

    【讨论】:

    • 哦,不。恐怕这会给出相同的输出。应用程序启动时侧边栏应该可见,然后当我们使用切换开关时,它应该折叠
    • 边栏在启动时可见。请复制上面提供的整个代码,然后重试。
    • 整洁。谢谢你。是添加shinywidgets的关键。
    • 您正在使用materialSwitch,它是library(shinyWidgets) 的一部分。
    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 2021-12-06
    • 2021-09-09
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    相关资源
    最近更新 更多