Not showing charts in menusubItems in shinydashboard (with highcharter) - shiny

I've been trying for hours on end to get this working however it does not work. I simply wish to render the charts in this dashboard for the different tabs. I have tried enclosing it with box(), using renderHighchart2 and highchartOutput2. The chart renders outside shiny just fine, what is wrong?
Edit: The charts I am referring to are the highcharts, not the valueboxes! Furthermore, the app does show the correct titles for each graph and fluidRow however fails to plot the highcharts
This is the code:
library(shiny)
library(shinydashboard)
library(highcharter)
library(tidyverse)
ui <- dashboardPage(
skin = "purple",
dashboardHeader(title = h4(HTML("Generic company name<br/>Something to analyze")),
titleWidth = 275),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard",icon = icon("dashboard"),
menuSubItem('Samenvatting', tabName = "samenvatting", icon = icon('atlas')),
menuSubItem('Statusverloop', tabName = "statusverloop", icon = icon('battery-three-quarters')),
menuSubItem('Tijdsverloop', tabName = "tijdsverloop", icon = icon("hourglass-end")),
menuSubItem('Affiliates', tabName = "affiliates", icon = icon("handshake")),
menuSubItem('Klanten informatie', tabName = "klanteninformatie", icon = icon("address-card"))
),
menuItem("Kijkglas",tabname = "kijkglas",icon = icon("search"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = 'samenvatting',
#contents
fluidRow(
valueBoxOutput("YTDnieuweA"),
valueBoxOutput("YTDomvangA")
),
fluidRow(
valueBoxOutput("YTDnieuweP") ,
valueBoxOutput("YTDomvangP")
),
fluidRow(
column( width = 6,h4("Wekelijkse statistieken", align = 'center'), highchartOutput('a') ),
column( width = 6,h4("Wekelijkse totale statistieken", align = 'center'), highchartOutput('b'))
)
),
tabItem(tabName = "statusverloop"
#Empty TODO:
),
tabItem(tabName = "tijdsverloop"
#EMPTY: TODO
),
tabItem(tabName = "affiliates",
fluidRow(
column( width = 6,h4("Affiliates over aanmeldingen", align = 'center'), highchartOutput('a') ),
column( width = 6,h4("Affiliates over passen", align = 'center'), highchartOutput('b'))
)
),
tabItem(tabName = "klanteninformatie",
fluidRow(
column( width = 4,h4("Wekelijkse statistieken", align = 'center'), highchartOutput('a') ),
column( width = 4,h4("Wekelijkse totale statistieken", align = 'center'), highchartOutput('b')),
column( width = 4,h4("Wekelijkse totale statistieken", align = 'center'), highchartOutput('a'))
)
)
)
)
)
server <- function(input, output) {
output$a <- renderHighchart2({
hc <- highcharts_demo() %>%
hc_rm_series("Berlin") %>%
hc_chart(type = 'line')
theme <- sandsignika = hc_theme_sandsignika()
hc <- hc %>% hc_add_theme(theme)
}
hc
})
output$b <- renderHighchart2({
hc <- highcharts_demo() %>%
hc_rm_series("Berlin") %>%
hc_chart(type = 'line')
theme <- hc_theme_economist()
hc <- hc %>% hc_add_theme(theme)
}
hc
})
}
shinyApp(ui,server)

In shiny, you can't refer to any output more than once in the UI.
Try storing the chart in an object (could use either a reactiveValues or reactive() to store it), then assigning that object into separate outputs.

Related

Tabs of the menuItem, in Shinydashboard, not working when put items inside

Lets take the example of the reference: https://rstudio.github.io/shinydashboard/structure.html#sidebar-menu-items-and-tabs. When put more items in the menuItem(), your associate tab don't works anymore. I tried in this simple modification in example below and just shown the widgets' tab:
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Dashboard",
tabName = "dashboard",
icon = icon("dashboard"),
selected = TRUE,
startExpanded = TRUE,
numericInput("num1",
"Put the First Number",
value = 1,
min = 0),
numericInput("num2",
"Put the Second Number",
value = 1,
min = 0)
),
menuItem("Widgets",
icon = icon("th"),
tabName = "widgets")
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content"),
fluidRow(
valueBoxOutput("box1", width = 6),
valueBoxOutput("box2", width = 6)
)
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
# Put them together into a dashboardPage
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output){
output$box1 <- renderValueBox({
valueBox(input$num1,
"First Number",
color = "aqua",
icon = icon("chart-line"))
})
output$box2 <- renderValueBox({
valueBox(input$num2,
"Second Number",
color = "aqua",
icon = icon("chart-line"))
})
}
shinyApp(ui, server)
That is because childfull menuItem behaves differently as noted here. Therefore, you need to define a menuItem or a menSubItem within that dashboard page so that your desired content can be displayed.
Try this
sidebarMenu(id = "tabs",
menuItem("Dashboard",
tabName = "dashboard",
icon = icon("tachometer-alt"),
selected = TRUE,
startExpanded = TRUE,
#icon = icon(fontawesome:::fa_tbl[[1]][505]),
menuItem("Sub-item 1", tabName = "subitem1"),
### menuSubItem("Sub-item 1", tabName = "subitem1"), ## it can be menuSubItem instead of menuItem
numericInput("num1",
"Put the First Number",
value = 1,
min = 0),
numericInput("num2",
"Put the Second Number",
value = 2,
min = 0)
),
menuItem("Widgets",
icon = icon("th"),
tabName = "widgets")
)
)
body <- shinydashboard::dashboardBody(
tabItems(
tabItem(tabName = "subitem1",
h2("Sub item1 tab content in Dashboard"),
fluidRow(
valueBoxOutput("box1", width = 6),
valueBoxOutput("box2", width = 6)
)
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
# Put them together into a dashboardPage
ui <- shinydashboard::dashboardPage(
skin = "green",
shinydashboard::dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output, session){
output$box1 <- renderValueBox({
valueBox(input$num1,
"First Number",
color = "aqua",
icon = icon("chart-line"))
})
output$box2 <- renderValueBox({
valueBox(input$num2,
"Second Number",
color = "aqua",
icon = icon("chart-line"))
})
observe({print(input$tabs)})
}
shinyApp(ui, server)

how can i use download button for download highchart in shiny

How can I use a download button instead of hc_exporting function to download a highchart in shiny?
library(shiny)
library(shinydashboard)
library(highcharter)
library(shinyWidgets)
RecruitmentFunneldb_struct <-
structure(list(
yyyy = c(2019L, 2019L, 2019L, 2019L, 2019L, 2019L),
stages = c(
"Phone Scrining",
"Interview",
"Offer",
"Pre-Onboarding",
"Post-Joining",
"Joined"
),
pop = c(8L, 25L, 23L, 32L, 8L, 4L)
),
row.names = c(NA,
6L),
class = "data.frame")
ui <-
dashboardPage(
dashboardHeader(
title = HTML("Analytic view - Recruitment"),
titleWidth = 280
),
dashboardSidebar(disable = T),
dashboardBody(fluidPage(fluidRow(
box(
title = fluidRow(
column(10, "Recruitment Funnel"),
column(
2,
align = "right",
downloadButton("download", label = NULL, class = "butt1"),
tags$head(
tags$style(
".butt1{display: inline-block;} .butt1{font-size: 20px;} .butt1{border: none;} .butt1{padding-top: 1px} .butt1{background-color: transperent .butt1{padding-right: 50px}}"
)
)
)
),
solidHeader = T,
width = 4,
collapsible = F,
highchartOutput("Recruitment_Funnel", height = "240px")
)
)))
)
server <- function(input, output, session) {
output$Recruitment_Funnel <- renderHighchart({
Reserve_Data <- RecruitmentFunneldb_struct %>% arrange(-pop)
Reserve_Data %>%
hchart("funnel", hcaes(x = stages, y = pop))
})
output$download <- downloadHandler(
filename = function() {
paste("Funnel", ".", "pdf")
},
content = function(file) {
pdf(file)
output$Recruitment_Funnel()
dev.off()
}
)
}
shinyApp(ui, server)

Change color theme DT table

How can we change color theme of DT table in shiny app? By default, it uses dark and light-grey color for alternate rows. I am using formatStyle(target = 'row', backgroundColor = c('yellow', 'red'). But it does not work as it works on columns only
library(shinydashboard)
header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
)
)
body <- dashboardBody(
box(
title = 'box', width = NULL, status = 'primary',
DT::dataTableOutput('table2')
)
)
ui<-dashboardPage(header, sidebar, body)
server = function(input, output) {
output$table2 = DT::renderDataTable(
iris, options = list(lengthChange = FALSE)
)
}
shinyApp(ui, server)
This should do, note that i left the header color white:
library(shinydashboard)
library(shiny)
library(DT)
header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
)
)
body <- dashboardBody(
tags$style(HTML('table.dataTable tr:nth-child(even) {background-color: pink !important;}')),
tags$style(HTML('table.dataTable tr:nth-child(odd) {background-color: yellow !important;}')),
tags$style(HTML('table.dataTable th {background-color: white !important;}')),
box(
title = 'box', width = NULL, status = 'primary',
DT::dataTableOutput('table2')
)
)
ui<-dashboardPage(header, sidebar, body)
server = function(input, output) {
output$table2 = DT::renderDataTable(
iris, options = list(lengthChange = FALSE)
)
}
shinyApp(ui, server)

sliderInput to appear in side-bar only when a Tab is selected

I am creating a Shinyapp in line of below template :
library(shinydashboard)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow()
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Tab1",
fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
),
tabItem(tabName = "Tab2",
fluidRow(navbarPage(id = 'ab', title = "",
tabPanel(title = "aa1", value = 'aa1', fluidRow()),
tabPanel(title = "aa2", value = 'aa2', fluidRow())))
)))
ui = dashboardPage(
dashboardHeader(title = "My tab"),
sidebar,
body
)
server = function(input, output) {}
shinyApp(ui = ui, server = server)
Now what I want basically, a sliderInput should appear in the Side panel only when TabPanel = 'aa2' from tabItem = 'Tab2'. Therefore user should not see that sliderInput if TabPanel = 'aa1' is selected.
So far I have tried something like below :
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow(sliderInput("aa5", "aa", value = 0.9, min = 0, max = 2, step=0.1))
)
)
However with this the sliderInput is visible for both tabPanel = "aa1" & tabPanel = "aa2", which I do not want.
Any suggestion, how can I make sliderInput is visible only for tabPanel = "aa2".
Thanks for your pointer.
#SBista thanks for pointer. Below is updated code. Thanks,
library(shinydashboard)
library(shinyjs)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Tab1", tabName="Tab1", selected=TRUE),
menuItem("Tab2", tabName = "Tab2")
),
conditionalPanel("input.tabs=='Tab1'",
fluidRow()
),
conditionalPanel("input.tabs=='Tab2'",
fluidRow(useShinyjs(),
column(12, sliderInput("aa4", "aa", value = 0.9, min = 0, max = 2, step=0.1)))
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "Tab1",
fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
),
tabItem(tabName = "Tab2",
fluidRow(navbarPage(id = 'ab', title = "",
tabPanel(title = "aa1", value = 'aa1', fluidRow()),
tabPanel(title = "aa2", value = 'aa2', fluidRow())))
)))
ui = dashboardPage(
dashboardHeader(title = "My tab"),
sidebar,
body
)
server = function(input, output) {
observe(toggle(id = "aa4", condition = ifelse(input$ab == 'aa2', TRUE, FALSE)))
}
shinyApp(ui = ui, server = server)

Fixing Width of WellPanel irrespective of Browser page width

I created a Shiny dashboard in line of below codes :
library(shinydashboard)
library(shiny)
sidebar <- dashboardSidebar(
sidebarMenuOutput("menu"),
conditionalPanel("input.tabs == 'ABC'",
fluidRow(
column(11, offset = 1, h5((' Note')))
)
),
conditionalPanel("input.tabs == 'ABC1'",
fluidRow(
column(11, offset = 1, style = "height:20px; color:rgb(30,144,255);", h1((' Update')))
)
))
body <- dashboardBody(
tabItems(
tabItem(tabName = "ABC1",br())
),
tabItems(
tabItem(tabName = "ABC",br(),
fixedRow(column(10, offset = 1, wellPanel()))
)
)
)
ui = dashboardPage(
dashboardHeader(title = "ABC"),
sidebar,
body
)
server = function(input, output){
output$menu <- renderMenu({
sidebarMenu(id="tabs",
menuItem("ABC", tabName="ABC", icon=icon("line-chart"), selected=TRUE),
menuItem("ABC1", tabName="ABC1", icon=icon("line-chart"))
)
})
}
shinyApp(ui = ui, server = server)
With above setup, the width of WellPanel getting changed if I resize my Browser. Therefore, this looks ugly when I see my App in bigger screen (e.g. 2560 x 1080). I want entire size of WellPanel will remain fixed. If Browser size is smaller than WellPanel then, some horizontal/vertical scroll bar will appear. And if bigger then, Wellpanel will stay in the top-middle part of the Browser.
Any idea what setting needs to be changed in my above code to achieve this.
Thanks,
I think this works:
library(shinydashboard)
library(shiny)
sidebar <- dashboardSidebar(
sidebarMenuOutput("menu"),
conditionalPanel("input.tabs == 'ABC'",
fluidRow(
column(11, offset = 1, h5((' Note')))
)
),
conditionalPanel("input.tabs == 'ABC1'",
fluidRow(
column(11, offset = 1, style = "height:20px; color:rgb(30,144,255);", h1((' Update')))
)
))
body <- dashboardBody(
tags$head(tags$style(type = "text/css"
, '#plotUI {min-width: 570px; max-width: 570px;overflow:auto;}'
)
)
,tabItems(
tabItem(tabName = "ABC1",br())
),
tabItems(
tabItem(tabName = "ABC",br(),
fluidRow(column(10, offset = 1
,div(class="thiswell"
, wellPanel(
style= "min-width: 600px;max-width: 600px;overflow:auto",
,uiOutput("plotUI")
)
)
) )
)
)
)
ui = dashboardPage(
dashboardHeader(title = "ABC"),
sidebar,
body
)
server = function(input, output){
output$menu <- renderMenu({
sidebarMenu(id="tabs",
menuItem("ABC", tabName="ABC", icon=icon("line-chart"), selected=TRUE),
menuItem("ABC1", tabName="ABC1", icon=icon("line-chart"))
)
})
output$plotUI<- renderUI({
plotOutput("thePlot")
})
output$thePlot<- renderPlot({
plot(1:10)
})
}
shinyApp(ui = ui, server = server)