Getting started

For example:

library(shiny)

server <- function(input, output) {
  output$message <- renderText({ "Hello world!" })
}

ui <- fluidPage(
  sidebarLayout( sidebarPanel( p("Nothing to see here; move along.") )
               , mainPanel( textOutput('message') )
               )
)

shinyApp(ui = ui, server = server)

The variables are:

  • production.pct: production as US share (percent)
  • consumption.btu: consumption in BTU
  • expenditures.dol: expenditure in US dollars
  • carbon.mT: CO2 production in metric tons

Roughing in the structure of the app

For this tutorial app our goals are:

  • A nice title up at the top of the page.
  • A short description of the app with a link to the source data.
  • A side bar that contains input controls. It should contain one input: a selection box that let's the user choose which variable is being visualized on the geo-spatial heatmap.
  • A main panel with a Leaflet visualization of the continental US. States will be colored based on the selected variable.
library(shiny)
library(leaflet)

server <- function(input, output) {
  
  # Do stuff.
  
}

ui <- fluidPage(
  
  # Page title
  
  # Link to source data
  
  # Sidebar with controls
  
  # Leaflet map
  
)

shinyApp(ui = ui, server = server)
ui <- fluidPage(
  
  # Page title
  titlePanel("US State Energy Profiles"),
  
  # Link to source data
  helpText( "Source data provided by the "
          , a("US Energy Information Administration", href = "http://www.eia.gov/state/")
          , "."
          ),
  
                 # Sidebar with controls
  sidebarLayout( sidebarPanel( selectInput( inputId = 'variable' 
                                          , label   = "Choose a variable"
                                          , choices = list()  # place holder!
                                          )
                             )
                 
                 # Leaflet map
               , mainPanel( leafletOutput( outputId = 'map') )
               )

)

shinyApp(ui = ui, server = server)

Source maps

states <- map("state", fill = TRUE, plot = FALSE)
states$names
##  [1] "alabama"                         "arizona"                        
##  [3] "arkansas"                        "california"                     
##  [5] "colorado"                        "connecticut"                    
##  [7] "delaware"                        "district of columbia"           
##  [9] "florida"                         "georgia"                        
## [11] "idaho"                           "illinois"                       
## [13] "indiana"                         "iowa"                           
## [15] "kansas"                          "kentucky"                       
## [17] "louisiana"                       "maine"                          
## [19] "maryland"                        "massachusetts:martha's vineyard"
## [21] "massachusetts:main"              "massachusetts:nantucket"        
## [23] "michigan:north"                  "michigan:south"                 
## [25] "minnesota"                       "mississippi"                    
## [27] "missouri"                        "montana"                        
## [29] "nebraska"                        "nevada"                         
## [31] "new hampshire"                   "new jersey"                     
## [33] "new mexico"                      "new york:manhattan"             
## [35] "new york:main"                   "new york:staten island"         
## [37] "new york:long island"            "north carolina:knotts"          
## [39] "north carolina:main"             "north carolina:spit"            
## [41] "north dakota"                    "ohio"                           
## [43] "oklahoma"                        "oregon"                         
## [45] "pennsylvania"                    "rhode island"                   
## [47] "south carolina"                  "south dakota"                   
## [49] "tennessee"                       "texas"                          
## [51] "utah"                            "vermont"                        
## [53] "virginia:chesapeake"             "virginia:chincoteague"          
## [55] "virginia:main"                   "washington:san juan island"     
## [57] "washington:lopez island"         "washington:orcas island"        
## [59] "washington:whidbey island"       "washington:main"                
## [61] "west virginia"                   "wisconsin"                      
## [63] "wyoming"
head(state.fips)
##   fips ssa region division abb    polyname
## 1    1   1      3        6  AL     alabama
## 2    4   3      4        8  AZ     arizona
## 3    5   4      3        7  AR    arkansas
## 4    6   5      4        9  CA  california
## 5    8   6      4        8  CO    colorado
## 6    9   7      1        1  CT connecticut
energySource <- read.csv("data/state-energy-data.csv")
row.names(energySource) <- energySource$State

energy <- energySource[as.character(state.fips$abb), -1]

Global application variables

energySource <- read.csv("data/state-energy-data.csv")
row.names(energySource) <- energySource$State
energy <- energySource[as.character(state.fips$abb), -1]

states  <- map("state", fill = TRUE, plot = FALSE)

server <- function(input, output) {
  
  # Do stuff.
  
}
ui <- fluidPage(
  
  # Page title
  titlePanel("US State Energy Profiles"),
  
  # Link to source data
  helpText( "Source data provided by the "
          , a("US Energy Information Administration", href = "http://www.eia.gov/state/")
          , "."
          ),
  
                 # Sidebar with controls
  sidebarLayout( sidebarPanel( selectInput( inputId = 'variable' 
                                          , label   = "Choose a variable"
                                          , choices = colnames(energy)
                                          )
                             )
                 
                 # Leaflet map
               , mainPanel( leafletOutput( outputId = 'map') )
               )

)

shinyApp(ui = ui, server = server)

Basic server implementation

server <- function(input, output) {
  
  output$map <- renderLeaflet({
    
    leaflet(data = states) %>% addTiles() %>%
      addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
    
  })
  
}

shinyApp(ui = ui, server = server)

Now we can use the colorNumeric function to create a color map to for one of our variables:

server <- function(input, output) {
  
  output$map <- renderLeaflet({
    
    pal <- colorNumeric(palette = "Blues", domain = energy$production.pct)
    
    leaflet(data = states) %>% addTiles() %>%
      addPolygons(fillColor = pal(energy$production.pct), stroke = FALSE)
    
  })
  
}

shinyApp(ui = ui, server = server)

Now all we need to do is add an input binding! Try it!