Banner

 

18 - Dashboards

Answers to exercises

1.
Create a dashboard to show the current conditions and temperature in both Celsius and Fahrenheit at a US weather station. Indicate when the data were last updated. Your will need the RWeather package. Consult the list of the U.S. National Oceanic and Atmospheric Administration (NOAA) weather stations to find the code for station.
library(shiny)
library(shinydashboard)
library(rwunderground)
library(measurements)

set_api_key("353685eb1f5194e3")

header <- dashboardHeader(title = 'Athens GA weather watch')
sidebar <- dashboardSidebar()

loc <-  set_location(airport_code = 'AHN')
weather <-  conditions(loc)
weather$tempC <- round(conv_unit(weather$temp,'F','C'),1) 
boxTemperatureF <-  box(title='Fahrenheit', weather$temp)
boxTemperatureC <-  box(title='Celsius',weather$tempC)
row1 <-  fluidRow(boxTemperatureF, boxTemperatureC)
body <- dashboardBody(row1)
ui <- dashboardPage(header,sidebar,body)

server <- function(input, output) {}

shinyApp(ui, server)
3.
Extend the previous dashboard. If the temperature is about 30C (86F), code the server function to give both temperature boxes a red background and if it is below 10C (50F) give both a blue background. Otherwise the color should be yellow.
library(shiny)
library(shinydashboard)
library(rwunderground)

header <- dashboardHeader(title =  'Current weather')
sidebar <- dashboardSidebar()
boxCity <-  box(selectInput('code', 'City:', choices = c('Atlanta' = 'ATL', 'Fairbanks' = 'FAI', 'New York' = 'JFK', 'Phoenix' = 'PHX', 'San Francisco' = 'SFO'), selected = 'ATL'))
#boxCondition <-  box(title = 'Current conditions: ', textOutput('condition'), background = 'blue')ndition <-
boxTime <-  box(textOutput('time'))
row1 <-  fluidRow(boxCity)
row2 <-  fluidRow(valueBoxOutput("vboxF"), valueBoxOutput("vboxC"))
body <- dashboardBody(row1,row2)

ui <- dashboardPage(header,sidebar,body)

server <- function(input, output) {
  output$vboxF <- renderValueBox({
    t <-
      as.numeric(conditions(set_location(airport_code = input$code))$temp)
    if (t  > 86)
    {
      valueBox(t, width = 3, subtitle = 'F', color = 'red')
    }
    else if (t < 50)
    {
      valueBox(t, width = 3, subtitle = 'F', color = 'blue')
    }
    else {
      valueBox(t, width = 3, subtitle = 'F', color = 'yellow')
    }
  })
  output$vboxC <- renderValueBox({
    t <-
      as.numeric(  output$vboxC <- renderValueBox({
        t <-
          as.numeric(round(conv_unit(conditions(set_location(airport_code = input$code))$temp,"F","C"),1))
        if (t  > 30)
        {
          valueBox(t, width = 3, subtitle = 'C', color = 'red')
        }
        else if (t < 10)
        {
          valueBox(t, width = 3, subtitle = 'C', color = 'blue')
        }
        else {
          valueBox(t, width = 3, subtitle = 'C', color = 'yellow')
        }
      })
      )
    if (t  > 30)
    {
      valueBox(t, width = 3, subtitle = 'C', color = 'red')
    }
    else if (t < 10)
    {
      valueBox(t, width = 3, subtitle = 'C', color = 'blue')
    }
    else {
      valueBox(t, width = 3, subtitle = 'C', color = 'yellow')
    }
  })
  
}

shinyApp(ui, server)
5.
Create a dashboard based on the WDI data to show a comparison of two countries on three or more of the most current measure of the state of the selected countries.

 

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

Date revised: 19-Oct-2016