在 R 中使用切换开关时如何默认打开边栏
How to have the sidebar open by default when using a toggle switch in R
我创建了以下闪亮的应用程序。该应用程序由以下部分组成
导入必要的库
library(shiny)
library(shinyjs)
接下来我们创建美国。请注意,我们正在使用 material 切换功能添加一个切换侧边栏选项,如下所示
# 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)
当应用程序 运行s 时,包含带有 bin 数量的滑块的侧边栏会折叠,因为侧边栏默认是折叠的。按下拨动开关可以访问相同的内容。
是否可以让侧边栏默认打开,然后在点击切换开关时将其折叠起来
谢谢
您可以使用 toggle
s 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)
我创建了以下闪亮的应用程序。该应用程序由以下部分组成
导入必要的库
library(shiny)
library(shinyjs)
接下来我们创建美国。请注意,我们正在使用 material 切换功能添加一个切换侧边栏选项,如下所示
# 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)
当应用程序 运行s 时,包含带有 bin 数量的滑块的侧边栏会折叠,因为侧边栏默认是折叠的。按下拨动开关可以访问相同的内容。
是否可以让侧边栏默认打开,然后在点击切换开关时将其折叠起来
谢谢
您可以使用 toggle
s 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)