closing sidebar in shiny dashboard - r

I am trying to make a multipage shiny dashboard. I would like the sidebar to collapse when you pick a page, with the ability to reopen it to pick a new page. For example, when you pick page 2 the sidebar collapses and you can reopen it later if you want to go back to page 1. Right now it is stuck open, i.e. when you click page 2 the sidebar does not collapse. I used useShinyjs(), which is what I thought makes it collapsible with no luck. Any help is much appreciated :)
library(shiny)
library(dplyr)
library(shiny)
library(shinydashboard)
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",
titleWidth = "100%"),
sidebar = dashboardSidebar(
useShinyjs(),
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,
boxPlus(plotlyOutput("All"),
status = 'warning',
width = 12,
solidHeader = TRUE,
collapsible = FALSE,
closable = FALSE,
collapsed = FALSE)))),
tabItem("pg2",
fluidRow(
column(width = 9,
boxPlus(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) {
observeEvent({
input$YR
},
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"))
})
)
}
shinyApp(ui = ui, server = server)

Only using useShinyjs() doesn't do the trick. It only sets up shinyjs, but you need to tell it what to do. The idea here is to add the class "sidebar-collapse" to the body, as this hides the sidebar. The sidebar should always been hidden if a tab was switched, so have to add an observer that listens if a tab was switched. Then you can use shinyjs to add the class with addClass. The input of the tabswitch is the id of the sidebarMenu:
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)
BTW: you also need the package shinydashboardPlus. Also, I removed your observer because I don't know what you want to achieve. Lastly, I reduced the width of the header, because otherwise the button to show the sidebar is hidden.
For more information how it works, have a look here and here.

Related

Navigate to a particular sidebar menu item in bs4Dash from Navbar?

I want to be able to navigate to a particular sidebar from the Navbar action button.
I know bs4Dash is a bootstrapping theme, but I am trying to customize a dashboard by moving the sidebar to the top of the navbar.
The only way I know possible is using action buttons like what I have done so far. However, in doing so I still haven't been able to successfully toggle to the right sidebar.
Here is the code
library(shiny)
library(bs4Dash)
shinyApp(
ui = bs4DashPage(
header = bs4DashNavbar(
skin = "light",
status = "white",
border = FALSE,
sidebarIcon = icon("bars"),
compact = FALSE,
controlbarIcon = icon("th"),
actionButton(inputId = "controlbarToggle", label = "Toggle Controlbar", class = "mx-2"),
actionButton(inputId = "socialcardToggle", label = "Toggle Tab 2", class = "mx-2"),
actionButton(inputId = "controlbarToggle", label = "Toggle Controlbar", class = "mx-2")
),
sidebar = bs4DashSidebar(
inputId = "sidebar",
disable = FALSE,
title = "My Dashboard",
skin = "light",
status = "primary",
brandColor = NULL,
url = NULL,
src = "https://cdn-icons-png.flaticon.com/512/2922/2922561.png",
elevation = 4,
opacity = 0.8,
expand_on_hover = TRUE,
bs4SidebarUserPanel(
image = "https://cdn-icons-png.flaticon.com/512/2922/2922510.png",
name = "Dennis"
),
bs4SidebarMenu(
id = "sidebarmenu",
bs4SidebarHeader("Menu1"),
bs4SidebarMenuItem(
"Tab 1",
tabName = "tab1",
icon = icon("user")
#startExpanded = FALSE
),
bs4SidebarMenuItem(
"Tab 2",
tabName = "tab2",
icon = icon("user")
#startExpanded = FALSE
)
)
),
controlbar = bs4DashControlbar(
"Text",
inputId = "controlbar",
disable = FALSE,
skin = "dark",
title = "Controlbar",
width = 250
),
footer = bs4DashFooter(left = "#Dennis", right = NULL),
title = "Basic Dashboard",
body = bs4DashBody(
bs4TabItems(
bs4TabItem(
tabName = "tab1",
bs4Card(
title = "Closable Box Tab 1 with dropdown",
closable = TRUE,
width = 12,
status = "warning",
solidHeader = FALSE,
collapsible = TRUE,
labelText = 1,
labelStatus = "danger",
labelTooltip = "Hi Bro!",
dropdownIcon = "wrench",
p("Box Content")
)
),
bs4TabItem(
tabName = "tab2",
bs4Card(
title = "Closable Box Tab 2 with dropdown",
closable = TRUE,
width = 12,
status = "warning",
solidHeader = FALSE,
collapsible = TRUE,
labelText = 1,
labelStatus = "danger",
labelTooltip = "Hi Bro!",
dropdownIcon = "wrench",
p("Box Content Tab 2")
)
)
)
)
),
server = function(input, output, session) {
observeEvent(input$controlbarToggle, {
updateControlbar(id = "controlbar")
})
observe(print(input$sidebar))
observe(print(input$sidebarmenu))
}
)
Thanks for your assistance in advance.

Why am I getting this code Error in tagAssert(body, type = "div", class = "content-wrapper") : argument "body" is missing, with no default

I have this issue, I already emailed my professor for help. All he said was I need to change things in the dashboard body. here is the code for the body. I don't see what I am missing. I basically copied the book line for line, but I cannot find the issue. and i have no clue what i am doing.
dashboardBody(
fluidRow(
column(width=6,
box(
title = "Select a Number",
solidHeader = TRUE,
background = "yellow",
status="warning",
width = NULL,
height = 312,
sliderInput(inputId = "number",
label = "",
value = 500, min = 25, max = 1000)),
box(title = "Histogram",
solidHeader=TRUE,
background = "light-blue",
status="primary",
width = NULL,
plotOutput("hist", height = 250)),
valueBoxOutput("meanBox"),
valueBoxOutput("medianBox"),
valueBoxOutput("sdBox")
),
tabItem(
tabName = "normal",
fluidRow(
box(title = "selecta Number",
solidHeader = TRUE,
collapsible = TRUE,
status = "warning",
sliderInput(inputId = "normnumber",
label = "",
value = 500,min = 25, max = 1000)),
box(title = "Destiny Plot",
solidHeader = TRUE,
background = "light-blue",
status = "primary",
plotOutput("density", height = 250)),
infoBoxOutput("meanInfoBox"),
infoBoxOutput("medianInfoBox"),
infoBoxOutput("sdInfoBox")
)
),
column(width = 6,
tabBox(
title = "Central Tendency",
id = "tabs1", height = 120, width = NULL,
tabPanel("Mean",
tabBox(
title = "Variability",
id = "tabs2",height = 120, width = NULL,
side = "right",
tabPanel("Variance",
h2(textOutput("vartext")),width = NULL),
tabPanel("Standard Deviation", h2(textOutput("sdtext")), width = NULL)
You are missing many ending (. Here is the fixed code:
library(shinydashboard)
library(shiny)
dashboardBody(
fluidRow(
column(width=6,
box(
title = "Select a Number",
solidHeader = TRUE,
background = "yellow",
status="warning",
width = NULL,
height = 312,
sliderInput(inputId = "number",
label = "",
value = 500, min = 25, max = 1000)),
box(title = "Histogram",
solidHeader=TRUE,
background = "light-blue",
status="primary",
width = NULL,
plotOutput("hist", height = 250)),
valueBoxOutput("meanBox"),
valueBoxOutput("medianBox"),
valueBoxOutput("sdBox")
),
tabItem(
tabName = "normal",
fluidRow(
box(title = "selecta Number",
solidHeader = TRUE,
collapsible = TRUE,
status = "warning",
sliderInput(inputId = "normnumber",
label = "",
value = 500,min = 25, max = 1000)),
box(title = "Destiny Plot",
solidHeader = TRUE,
background = "light-blue",
status = "primary",
plotOutput("density", height = 250)),
infoBoxOutput("meanInfoBox"),
infoBoxOutput("medianInfoBox"),
infoBoxOutput("sdInfoBox")
)
),
column(width = 6,
tabBox(
title = "Central Tendency",
id = "tabs1", height = 120, width = NULL,
tabPanel("Mean",
tabBox(
title = "Variability",
id = "tabs2",height = 120, width = NULL,
side = "right",
tabPanel("Variance",
h2(textOutput("vartext")),width = NULL),
tabPanel("Standard Deviation", h2(textOutput("sdtext")), width = NULL)
)
)
)
)
)
)

Working through "R Projects for Dummies" and ran into error message

As the title says. I have entered the code exactly as it is the book, but I run into this error message: "Error in shiny::tabsetPanel(..., id=id, selected = selected) : argument is missing, with no default"
I have no idea what is missing.
Here is the code:
library(shinydashboard)
ui<- dashboardPage(
dashboardHeader(title= "Uniform Distribution"),
dashboardSidebar(),
dashboardBody(
fluidRow(
column(width =6,
box(title = "Select a Number",
solidHeader = TRUE,
background = "yellow",
status = "warning",
width = NULL,
height = 312,
sliderInput(inputId = "number",
label = "",
value = 500, min = 25, max = 1000)),
box(
background= "light-blue",
solidHeader = TRUE,
status ="primary",
title = "Histogram",
width = NULL,
plotOutput("hist", height = 250))),
column(width = 6,
tabBox(
title ="Central Tendency",
id ="tabs1", height = 120, width =NULL,
tabPanel("Mean", h2(textOutput("meantext")),width=NULL),
tabPanel("Median", h2(textOutput("mediantext")),width=NULL),
),
tabBox(
title ="Variability",
id ="tabs2", height = 120, width =NULL,
tabPanel("Variance", h2(textOutput("vartext")),width=NULL),
tabPanel("Standard Deviation", h2(textOutput("sdtext")),width=NULL))
)
)
))
server <- function(input,output){
histdata <- reactive({runif(input$number, min=0, max = 1)})
output$hist <- renderPlot({
hist(histdata(), xlab = "Value",
main= paste(input$number,"random values between 0 and 1"))
})
output$meantext <- renderText({
paste("Mean =", round(mean(histdata()),3))})
output$mediantext <- renderText({
paste("Median =", round(median(histdata()),3))})
output$vartext <- renderText({
paste("Variance =", round(var(histdata()),3))})
output$sdtext <- renderText({
paste("Standard Deviation =", round(sd(histdata()),3))})
}
shinyApp(ui, server)
You have tabPanels outside of tabsetPanels. Your tabPanels need to wrapped inside tabsetPanel() or navBarPage()
See below:
ui<- dashboardPage(
dashboardHeader(title= "Uniform Distribution"),
dashboardSidebar(),
dashboardBody(
fluidRow(
column(width =6,
box(title = "Select a Number",
solidHeader = TRUE,
background = "yellow",
status = "warning",
width = NULL,
height = 312,
sliderInput(inputId = "number",
label = "",
value = 500, min = 25, max = 1000)),
box(
background= "light-blue",
solidHeader = TRUE,
status ="primary",
title = "Histogram",
width = NULL,
plotOutput("hist", height = 250))),
column(width = 6,
tabBox(
title ="Central Tendency",
id ="tabs1", height = 120, width =NULL,
tabsetPanel(
tabPanel("Mean", h2(textOutput("meantext")),width=NULL),
tabPanel("Median", h2(textOutput("mediantext")),width=NULL)
)
),
tabBox(
title ="Variability",
id ="tabs2", height = 120, width =NULL,
tabsetPanel(
tabPanel("Variance", h2(textOutput("vartext")),width=NULL),
tabPanel("Standard Deviation", h2(textOutput("sdtext")),width=NULL)
)
)
)
)
))

shinydashboard column layout of box()

I have a shinydashboard that looks like this:
# Packages
library(shinydashboard)
library(tidyverse)
library(readxl)
library(scales)
theme_set(theme_light())
header <- dashboardHeader(
title = "Test App",
titleWidth = 215
)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Test Tab", tabName = "test_tab",
icon = icon("paper-plane"), startExpanded = TRUE)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "test_tab",
fluidRow(
column(width = 4,
h2("Column X"),
valueBoxOutput("first_value", width = NULL),
box(flexdashboard::gaugeOutput("second_value", width = "90%", height = "100px"),
title = "Second Value", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = NULL
)
),
column(width = 8,
h2("Column Y"),
box(
title = "#3", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = 4
),
box(
title = "#4", status = "primary", solidHeader = TRUE,
collapsible = FALSE, width = 4
)
)
),
fluidRow(
h2("Row A"),
column(width = 12,
box(title = "Third Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Fourth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Fifth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Sixth Value", status = "primary", solidHeader = TRUE,
width = 2.4),
box("Seventh Value", status = "primary", solidHeader = TRUE,
width = 2.4)
)
)
)
)
)
# Put them together into a dashboardPage
ui <- dashboardPage(skin = "blue", header = header,
sidebar = sidebar,
body = body)
server <- function(input, output) {
output$first_value <- renderValueBox({
valueBox(
comma_format()(100000),
subtitle = "First Value",
icon = icon("list"), color = "purple"
)
})
output$second_value = flexdashboard::renderGauge({
flexdashboard::gauge(0.12 * 100,
symbol = '%',
min = 0,
max = 100)
})
}
shinyApp(ui, server)
I'm trying to get the box()es below RowA to line up in a five column format, like this:
Third Value | Fourth Value | Fifth Value | Sixth Value
I'm not sure how I'd do that here. I've tried to put 5 boxes inside column() inside a fluidRow() which should work, but unfortunately, the boxes keep on showing up horizontally...
Is there anyway to display the boxes in column format? If not, can you guide me to other functions that resemble what I have?
You've specified your column widths for each of these five boxes to be 2.4 (12/5 I guess).
Column widths should be whole numbers. If you substitute 2 for 2.4 it'll work fine.
Note that you will have an empty column to the right of box 5 corresponding to the 2 (12 - 2*5) positions left over.
This is based on bootstrap column layout. For more information see this tutorial.

Error in dynamically choosing a dataset in shiny dashboard

I am a newbie to shiny dashboard. I want to dynamically select a dataset among different datasets uploaded and use it to display the dataset.
I have written the below code but i am getting an error,
Warning: Error in DT::datatable: 'data' must be 2-dimensional (e.g. data frame or matrix)
ui
ui <- dashboardPage(skin = "yellow",
dashboardHeader(title = "Analytics Workbench 2.0", titleWidth = 250,
dropdownMenuOutput("msgs")),
dashboardSidebar(
sidebarMenu(
fileInput("Table1", "Train Data"),
fileInput("Table2", "Test Data"),
menuItem("Variable Analysis", icon = icon("edit"),
menuSubItem("Uni-Variate Analysis"),
menuSubItem("Multi-Variate Analysis"))
)
),
dashboardBody(
fluidPage(
fluidRow(
column(12, box(title = "Train Data", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, DT::DTOutput("dtable1")),
box(title = "Test Data", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, DT::DTOutput("dtable2")))),
fluidRow(
column(12, box(title = "Structure", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, verbatimTextOutput("str1")),
box(title = "Structure", width = 6, solidHeader = TRUE, status = "primary",
collapsible = TRUE, verbatimTextOutput("str2"))))
)
)
)
server
server <- function(input, output) {
Train <- reactive({
if (is.null(input$Table1)) return(NULL)
read.table(input$Table1$datapath, fill = TRUE, header=T, sep=",", na.strings = c(""," ",NA))
})
Test <- reactive({
if (is.null(input$Table2)) return(NULL)
read.table(input$Table2$datapath, fill = TRUE, header=T, sep=",", na.strings = c(""," ",NA))
})
dataset_1 <- reactive({
switch(input$Datasets,
"Train" = Train,
"Test" = Test)
})
output$dtable2 <- DT::renderDT({
DT::datatable(dataset_1(), options = list(scrollX = TRUE))
}) }
Please help me solve this issue.
Thanks Balaji

Resources