R 关闭闪亮仪表板中的侧边栏
我正在尝试制作一个多页的闪亮仪表板。我想边栏崩溃时,你选择一个页面,有能力重新打开它选择一个新的页面。例如,当您选择第2页时,侧边栏将折叠,如果您想返回第1页,可以稍后重新打开侧边栏。现在它被卡在打开位置,即当您单击第2页时,侧边栏不会折叠。我使用useShinyjs(),我认为这会让它在没有运气的情况下崩溃。非常感谢您的帮助:)R 关闭闪亮仪表板中的侧边栏,r,shiny,R,Shiny,我正在尝试制作一个多页的闪亮仪表板。我想边栏崩溃时,你选择一个页面,有能力重新打开它选择一个新的页面。例如,当您选择第2页时,侧边栏将折叠,如果您想返回第1页,可以稍后重新打开侧边栏。现在它被卡在打开位置,即当您单击第2页时,侧边栏不会折叠。我使用useShinyjs(),我认为这会让它在没有运气的情况下崩溃。非常感谢您的帮助:) 库(闪亮) 图书馆(dplyr) 图书馆(闪亮) 图书馆(shinydashboard) 图书馆(shinyjs) 图书馆(shinyWidgets) 图书馆(shi
库(闪亮)
图书馆(dplyr)
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(shinyjs)
图书馆(shinyWidgets)
图书馆(shinyBS)
图书馆(绘本)
仅使用useShinyjs()
存储不会起作用。它只设置shinyjs,但您需要告诉它该做什么。这里的想法是将类“边栏折叠”添加到主体中,因为这样会隐藏边栏。如果切换了选项卡,侧边栏应该始终隐藏,因此必须添加一个观察者,以便在切换选项卡时进行侦听。然后可以使用shinyjs添加带有addClass
的类。选项卡开关的输入是侧栏菜单的id
:
library(shiny)
library(dplyr)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(shinyWidgets)
library(shinyBS)
library(plotly)
Stores <- data.frame(Store = c("Store 1", "Store 2", "Store 3", "Store 4", "Store 5"),
Sales = c(8247930, 423094, 204829, 903982, 7489472, 429085, 208955, 7492852, 5285034, 2958275,1598753, 28487593, 4892049, 583042, 509275, 5904728, 5098325, 5920947, 4920946, 2049583),
Avg_cust = c(325,542,582,482,904, 594, 304, 493, 690, 403, 694, 104, 493, 596, 403, 506, 304, 305, 632, 478),
Year = c(rep(2012,5), rep(2013,5), rep(2014,5), rep(2015,5)))
ui <- dashboardPage(
header = dashboardHeader(
title = "Store Performance"),
sidebar = dashboardSidebar(
width = 200,
collapsed = FALSE,
sidebarMenu(id = "tabs",
menuItem("Page 1", tabName = "pg1"),
menuItem("Page 2", tabName = "pg2"))),
skin = "black",
body = dashboardBody(
useShinyjs(),
tabItems(
tabItem("pg1",
fluidRow(
column(width = 3,
box(
title = "Options",
status = 'warning',
solidHeader = TRUE,
width = 12,
collapsible = FALSE,
collapsed = FALSE,
pickerInput(
inputId = "YR",
label = "Year:",
choices = c(2012,2013,2014,2015),
selected = 2015,
multiple = FALSE))),
column(width = 9,
box(plotlyOutput("All"),
status = 'warning',
width = 12,
solidHeader = TRUE,
collapsible = FALSE,
closable = FALSE,
collapsed = FALSE)))),
tabItem("pg2",
fluidRow(
column(width = 9,
box(title = "Add graph here",
width = 12,
status = "warning",
solidHeader = TRUE,
collapsible = FALSE,
closable = FALSE,
collapsed = FALSE)),
column(width = 3,
box(
title = "Options",
status = 'warning',
solidHeader = TRUE,
width = 12,
collapsible = FALSE,
collapsed = FALSE,
pickerInput(
inputId = "st",
label = "Store:",
choices = unique(Stores$Store),
selected = "Store 1",
multiple = FALSE
))))))))
server <- function(input, output) {
output$All <- renderPlotly({
plot_ly(Stores[Stores$Year == input$YR,], x = ~Avg_cust, y = ~Sales,
hoverinfo = "text", text = ~Store)%>%
layout(title = "Store Performance",
xaxis = list(title = "Customers"),
yaxis = list(title = "Sales"))
})
observeEvent(input$tabs, {
addClass(selector = "body", class = "sidebar-collapse")
})
}
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(dplyr)
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(shinydashboardPlus)
图书馆(shinyjs)
图书馆(shinyWidgets)
图书馆(shinyBS)
图书馆(绘本)
商店
library(shiny)
library(dplyr)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(shinyWidgets)
library(shinyBS)
library(plotly)
Stores <- data.frame(Store = c("Store 1", "Store 2", "Store 3", "Store 4", "Store 5"),
Sales = c(8247930, 423094, 204829, 903982, 7489472, 429085, 208955, 7492852, 5285034, 2958275,1598753, 28487593, 4892049, 583042, 509275, 5904728, 5098325, 5920947, 4920946, 2049583),
Avg_cust = c(325,542,582,482,904, 594, 304, 493, 690, 403, 694, 104, 493, 596, 403, 506, 304, 305, 632, 478),
Year = c(rep(2012,5), rep(2013,5), rep(2014,5), rep(2015,5)))
ui <- dashboardPage(
header = dashboardHeader(
title = "Store Performance"),
sidebar = dashboardSidebar(
width = 200,
collapsed = FALSE,
sidebarMenu(id = "tabs",
menuItem("Page 1", tabName = "pg1"),
menuItem("Page 2", tabName = "pg2"))),
skin = "black",
body = dashboardBody(
useShinyjs(),
tabItems(
tabItem("pg1",
fluidRow(
column(width = 3,
box(
title = "Options",
status = 'warning',
solidHeader = TRUE,
width = 12,
collapsible = FALSE,
collapsed = FALSE,
pickerInput(
inputId = "YR",
label = "Year:",
choices = c(2012,2013,2014,2015),
selected = 2015,
multiple = FALSE))),
column(width = 9,
box(plotlyOutput("All"),
status = 'warning',
width = 12,
solidHeader = TRUE,
collapsible = FALSE,
closable = FALSE,
collapsed = FALSE)))),
tabItem("pg2",
fluidRow(
column(width = 9,
box(title = "Add graph here",
width = 12,
status = "warning",
solidHeader = TRUE,
collapsible = FALSE,
closable = FALSE,
collapsed = FALSE)),
column(width = 3,
box(
title = "Options",
status = 'warning',
solidHeader = TRUE,
width = 12,
collapsible = FALSE,
collapsed = FALSE,
pickerInput(
inputId = "st",
label = "Store:",
choices = unique(Stores$Store),
selected = "Store 1",
multiple = FALSE
))))))))
server <- function(input, output) {
output$All <- renderPlotly({
plot_ly(Stores[Stores$Year == input$YR,], x = ~Avg_cust, y = ~Sales,
hoverinfo = "text", text = ~Store)%>%
layout(title = "Store Performance",
xaxis = list(title = "Customers"),
yaxis = list(title = "Sales"))
})
observeEvent(input$tabs, {
addClass(selector = "body", class = "sidebar-collapse")
})
}
shinyApp(ui = ui, server = server)