Banner

 

19 - Dashboards
Slide Exercises

Add three more boxes to the dashboard.
library(shiny)
library(shinydashboard)
library(quantmod)
header <- dashboardHeader(title = 'Apple stock watch')
sidebar <- dashboardSidebar()
boxLatest <- box(paste('Latest price ',getQuote('AAPL')$Last))
boxChange <-  box(paste('Change ',getQuote('AAPL')$Change))
boxHigh <-  box(paste('High ',getQuote('AAPL')$High))
boxLow <-  box(paste('Low ',getQuote('AAPL')$Low))
boxVolume <-  box(paste('Volume ',getQuote('AAPL')$Volume))
row <- fluidRow(boxLatest,boxChange,boxHigh, boxLow, boxVolume)
body <- dashboardBody(row)
ui <- dashboardPage(header,sidebar,body)
server <- function(input, output) {}
shinyApp(ui, server)
Using ClassicModels, build a dashboard to report total orders by value and number for a given year and month.
library(DBI)
library(shiny)
library(shinydashboard)
header <- dashboardHeader(title = 'Orders')
side <-
  sidebarMenu(menuItem("KPIs", tabName = "dashboard", icon = icon("calculator")))
pullCurr1 <-
  selectInput('year', 'Year:', choices = c('2003', '2004', '2005'))
pullCurr2 <-
  selectInput(
    'month', 'Month', choices = c(
      'January' = '1', 'February' = '2', 'March' = '3', 'April' = '4', 'May' = '5', 'June' = '6',
      'July' = '7', 'August' = '8', 'September' = '9', 'October' = '10', 'November' = '11', 'December' = '12'))
sidebar <-
  dashboardSidebar(sidebarMenu(side),pullCurr1, pullCurr2)
info1 <- valueBox('Value',  h3(textOutput("sales")), color = 'red')
info2 <-
  valueBox('Number',  h3(textOutput("orders")), color = 'yellow')
row1 <-  fluidRow(info1,info2)
body <- dashboardBody(row1)
ui <- dashboardPage(header,sidebar,body)
server <- function(input, output) {
  # make the connection
  conn <-
    dbConnect(
      RMySQL::MySQL(), "wallaby.terry.uga.edu", dbname = "ClassicModels", user = "student", password =
        "student")
  output$sales <-  renderText({
    sql <-
      paste0(
        'SELECT FORMAT(SUM(quantityOrdered*priceEach),2) FROM Orders, OrderDetails WHERE Orders.orderNumber = OrderDetails.orderNumber AND YEAR(orderDate) = ',input$year, ' AND month(orderDate) = ', input$month)
    query <-  dbGetQuery(conn,sql)[1,1]
  })
  output$orders <-  renderText({
    sql <-
      paste0(
        'SELECT COUNT(*) FROM Orders WHERE YEAR(orderDate) = ',input$year, ' AND MONTH(orderDate) = ', input$month)
    query <-  dbGetQuery(conn,sql)[1,1]
  })
}

shinyApp(ui, server)

This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson
For questions and comments please contact the author

Date revised: 02-Dec-2022