HydroApp Script

Download server.R script here.
Download ui.R script here.

server.R:

library(shiny)
library(XML)
library(googleVis)
library(gsubfn)
library(httr)

### OVERHEAD
noData = logical()
urlEncode <- function(URL) {
gsubfn(".", list("_" = "%5F", "." = "%2E","+" = "%2B","!" = "%21",
                 "*" = "%2A","'" = "%27"), c(URL))
}

### Define server logic required to plot various players against 3000 hit club
shinyServer(function(input, output, session) {

  
  makeURL <- reactive({
    inStateID <- input$stateID
    inStateID.url <-URLencode(paste("US", inStateID, sep=":"), reserved = T)
    inParmID <- input$parmID
    inParmID.url <- urlEncode(inParmID)
    
    url <- paste("http://www.waterqualitydata.us/Station/search?countrycode=US",
                 "&statecode=", inStateID.url,
                 "&characteristicName=", inParmID.url,                   
                 "&siteType=Stream&mimeType=csv&zip=no", sep="")
    print(url)    
    return(url)
  })
  
  testURL <- reactive({
    url <- makeURL()
    urlHeaders <- HEAD(url)$header  
    warnings <- which(names(urlHeaders) %in% "warning")
    
    if(!length(warnings)){
      noData = FALSE
      summary <- cbind(paste("Total Site Count: ",
                             urlHeaders$'total-site-count'))
      print(summary)
      print(noData)
    }
    if(length(warnings) > 0){
      noData = TRUE
      summary <- paste("WARNING :", unlist(urlHeaders)[warnings])
      print(summary)
    }
    return(list(noData, summary))
    
  })
  
  # Read in data from file
  stateStationsInput <- reactive({
    if(testURL()[[1]] == F){
      url <- makeURL()
      stateStations = read.csv(url, colClasses = "character", sep =",",
                               na.strings = "", fill = F, allowEscapes = F,
                               header = T)
      
      stationLatLong <- paste(stateStations$LatitudeMeasure,
                              stateStations$LongitudeMeasure, sep=":")      
      stationTip <- paste(stateStations$MonitoringLocationName, 
                          stateStations$MonitoringLocationIdentifier, sep="<BR>")
      
      data.frame(stationLatLong, stationTip)
    }
  })
  
  output$stationMap <- renderGvis({
    if(testURL()[[1]] == F){
      mapData <- stateStationsInput()
      gvisMap(mapData, "stationLatLong", "stationTip",
             options=list(showTip=TRUE, showLine=FALSE, enableScrollWheel=TRUE,
             mapType='terrain', useMapTypeControl=TRUE))
    }
    
  })
  
})

ui.R:

library(shiny)
library(XML)

url <- readLines("http://www.waterqualitydata.us/Codes/Characteristicname?mimeType=xml")
udoc <- htmlParse(url, asText = T)
udoc.nodes <- getNodeSet(udoc, path = "//code")
parmList <- matrix(unlist(lapply(udoc.nodes, function(x) c(xmlAttrs(x)[1]))),
                   byrow = T)

# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
  
  # Application title
  headerPanel("USGS Water Quality Analyzer"),
  
  # Sidebar with controls to select a member of the 3,000 hit club
  # and input a non-member and plot their hit trajectory
  sidebarPanel(
    
    h4("Enter US State"),
    
    # To text input to select non-3000 hit member to plot hit trajectory
    #textInput("stateID", "USGS State (abbr.):", value = ""),
    selectInput("stateID", "Select State:",
                c("ALABAMA" = "01", 
                    "ALASKA" = "02", 
                    "AMERICAN SAMOA" = "60", 
                    "ARIZONA" = "04",
                    "ARKANSAS" = "05", 
                    "BAKER ISLAND" = "81", 
                    "CALIFORNIA" = "06", 
                    "COLORADO" = "08", 
                    "CONNECTICUT" = "09", 
                    "DELAWARE" = "10", 
                    "DISTRICT OF COLUMBIA" = "11",
                    "FLORIDA" = "12",
                    "GEORGIA" = "13",
                    "GUAM" = "66", 
                    "HAWAII" = "15", 
                    "HOWLAND ISLAND" = "84", 
                    "IDAHO" = "16", 
                    "ILLINOIS" = "17", 
                    "INDIANA" = "18", 
                    "IOWA" = "19", 
                    "JARVIS ISLAND" = "86", 
                    "JOHNSTON ATOLL" = "67", 
                    "KANSAS" = "20", 
                    "KENTUCKY" = "21", 
                    "KINGMAN REEF" = "89", 
                    "LOUISIANA" = "22", 
                    "MAINE" = "23", 
                    "MARSHALL ISLANDS" = "68", 
                    "MARYLAND" = "24", 
                    "MASSACHUSETTS" = "25", 
                    "MICHIGAN" = "26", 
                    "MICRONESIA" = "64", 
                    "MIDWAY ISLANDS" = "71", 
                    "MINNESOTA" = "27", 
                    "MISSISSIPPI" = "28", 
                    "MISSOURI" = "29", 
                    "MONTANA" = "30", 
                    "N. MARIANA ISLANDS" = "69", 
                    "NAVASSA ISLAND" = "76", 
                    "NEBRASKA" = "31", 
                    "NEVADA" = "32", 
                    "NEW HAMPSHIRE" = "33", 
                    "NEW JERSEY" = "34", 
                    "NEW MEXICO" = "35", 
                    "NEW YORK" = "36", 
                    "NORTH CAROLINA" = "37", 
                    "NORTH DAKOTA" = "38", 
                    "OHIO" = "39", 
                    "OKLAHOMA" = "40", 
                    "OREGON" = "41", 
                    "PALAU" = "70", 
                    "PALMYRA ATOLL" = "95", 
                    "PENNSYLVANIA" = "42", 
                    "PUERTO RICO" = "72", 
                    "RHODE ISLAND" = "44", 
                    "SOUTH CAROLINA" = "45", 
                    "SOUTH DAKOTA" = "46", 
                    "TENNESSEE" = "47", 
                    "TEXAS" = "48", 
                    "UNSPECIFIED" = "00", 
                    "UTAH" = "49", 
                    "VERMONT" = "50", 
                    "VIRGIN ISLANDS" = "78",
                    "VIRGINIA" = "51", 
                    "WAKE ISLAND" = "79", 
                    "WASHINGTON" = "53",
                    "WEST VIRGINIA" = "54",
                    "WISCONSIN" = "55", 
                    "WYOMING" = "56"),
                selected = "Alabama"),
    
    #Output list of Geochemical parameters to analyze
    #uiOutput("getPar"),
    selectInput("parmID", "Choose Water Quality Parameter", parmList),
    # Button to update plot output
    submitButton("Update View")
  ),
  
  #Show the output plot of the hit trajectory
  mainPanel(
    tabsetPanel(
      tabPanel("Station Map",
               textOutput("header"),               
               htmlOutput("stationMap"))
    )
  )
))
Advertisements

One thought on “HydroApp Script

  1. Pingback: HydroApp Part 1: Using Shiny and googleVis to downloading and displaying US water quality data | Chit Chat R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s