Dashboards – Part Two “I think, aesthetically, car design is so interesting - the dashboards, the steering wheels, and the beauty of the mechanics. I don't know how any of it works, I don't want to know, but it's inspirational.” Paloma Picasso
Pick Up Where We Left Today we will pick up where we left last time. More specifically, we will improve our dashboard illustrating the results (i.e., histogram) of the sentiment analysis we performed about @delta on Twitter. We will do so by adding content to our sidebar (table with text), by customizing our dashboard (color + infobox), by making it dynamic, and by adding a time series component!
Finally input @delta results to your dashboard (server) library(shiny) library(shinydashboard) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar() body <- dashboardBody(fluidRow( box(plotOutput("plot1", height = 250)), box( title = "Controls", sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))) ui <- dashboardPage(header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) }) } shinyApp(ui, server)
Finally input @delta results to your dashboard (server)
Add items to your sidebar library(shiny) library(shinydashboard) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar( sidebarMenu( menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Table", tabName = "sentiment", icon = icon("table")) ) body <- dashboardBody(fluidRow( box(plotOutput("plot1", height = 250)), box( title = "Controls", sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))) ui <- dashboardPage(header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) }) } shinyApp(ui, server)
Add items to your sidebar
Add table to your sidebar tab library(shiny) library(shinydashboard) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar(sidebarMenu( menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Table", tabName = "sentiment", icon = icon("table")) )) body <- dashboardBody(tabItems( # First tab content tabItem(tabName = "dashboard", fluidRow(box(plotOutput("plot1", height = 250)), box(title = "Controls", sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))), # Second tab content tabItem(tabName = "sentiment", dataTableOutput("mytable") ))) ui <- dashboardPage(header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) output$mytable <- renderDataTable({delta_scores}) }) } shinyApp(ui, server)
Add items to your sidebar
Customizing dashboard library(shiny) library(shinydashboard) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar(sidebarMenu( menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Table", tabName = "sentiment", icon = icon("table")) )) body <- dashboardBody(tabItems( # First tab content tabItem(tabName = "dashboard", fluidRow(box(title = "Histogram", status = "primary", solidHeader = TRUE, collapsible = TRUE, plotOutput("plot1", height = 250)), box(title = "Controls", status = "warning", solidHeader = TRUE, collapsible = TRUE, sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))), # Second tab content tabItem(tabName = "sentiment", dataTableOutput("mytable") ))) ui <- dashboardPage(skin = "green", header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) output$mytable <- renderDataTable({delta_scores}) }) } shinyApp(ui, server)
Customize dashboard
Make it dynamic/add infobox library(shiny) library(shinydashboard) library(quantmod) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar(sidebarMenu( menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Table", tabName = "sentiment", icon = icon("table")) , menuItem("Stock", tabName = "stock", icon = icon("dollar")) )) body <- dashboardBody(tabItems( # First tab content tabItem(tabName = "dashboard", fluidRow(box(title = "Histogram", status = "primary", solidHeader = TRUE, collapsible = TRUE, plotOutput("plot1", height = 250)), box(title = "Controls", status = "warning", solidHeader = TRUE, collapsible = TRUE, sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))), # Second tab content tabItem(tabName = "sentiment", dataTableOutput("mytable")) , # Third tab content tabItem(tabName = "stock", fluidRow(infoBox(title = "Latest", icon('dollar'), getQuote('DAL')$Last, color='red'), infoBoxOutput("deltaBox")) ) )) ui <- dashboardPage(skin = "green", header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) output$mytable <- renderDataTable({delta_scores}) output$deltaBox <- renderInfoBox({infoBox("Website", icon('plane'), href = 'http://ir.delta.com/shareholder-resources/investor-updates/default.aspx', color='black')}) }) } shinyApp(ui, server)
Make it dynamic/add infobox
Add times series library(shiny) library(shinydashboard) library(quantmod) library(dygraphs) header <- dashboardHeader(title = "@delta on Twitter") sidebar <- dashboardSidebar(sidebarMenu( menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")), menuItem("Table", tabName = "sentiment", icon = icon("table")) , menuItem("Stock", tabName = "stock", icon = icon("dollar")) )) body <- dashboardBody(tabItems( # First tab content tabItem(tabName = "dashboard", fluidRow(box(title = "Histogram", status = "primary", solidHeader = TRUE, collapsible = TRUE, plotOutput("plot1", height = 250)), box(title = "Controls", status = "warning", solidHeader = TRUE, collapsible = TRUE, sliderInput("Slider", "Number of Messages:", 1, 1500, 500)))), # Second tab content tabItem(tabName = "sentiment", dataTableOutput("mytable")) , # Third tab content tabItem(tabName = "stock", fluidRow(infoBox(title = "Latest", icon('dollar'), getQuote('DAL')$Last, color='red'), infoBoxOutput("deltaBox"), box(title = "Closing share price", width = 12, heigth = NULL, dygraphOutput('delta') )) ) )) ui <- dashboardPage(skin = "green", header,sidebar,body) server <- function(input, output) { delta_scores <- score.sentiment(delta_text, pos.words, neg.words) output$plot1 <- renderPlot({ delta <- delta_scores$score[seq_len(input$Slider)] hist(delta) output$mytable <- renderDataTable({delta_scores}) output$deltaBox <- renderInfoBox({infoBox("Website", icon('plane'), href = 'http://ir.delta.com/shareholder-resources/investor-updates/default.aspx', color='black')}) output$delta <- renderDygraph({dygraph(Cl(get(getSymbols('DAL'))))})}) } shinyApp(ui, server)
Add times series
Add times series
Conclusion In the last two days you learned the basic structure and use of shinydashboard. It has many options, and you will need to consult the online documentation and examples to learn more about creating dashboards Just know that shinydashboard is REALLY powerful!