Visualize temperature data directly from web

Visualize temperature data directly from web
by Tamas Marko

show with app
library(shiny)
library(rCharts)

shinyServer(
  function(input, output, session) {
    loadHTMLsource <- reactive({
      readLines('http://academic.udayton.edu/kissock/http/Weather/citylistWorld.htm')
    })
    
    getCountryName <- reactive({
      webpage <- loadHTMLsource()
      web_content <- webpage[grep("<b>", webpage)]
      
      Begin = regexpr('b>', web_content) +2
      End = regexpr('\\(', web_content) -2
      
      country_name = substr(web_content, Begin, End)
      country_name = country_name[country_name != ""]
      country_name = gsub("</b> <b", "", country_name)
    }) 
    
    getCountryCode <- reactive({
      webpage <- loadHTMLsource()
      web_content <- webpage[grep("\"gsod95-current", webpage)]
      
      Begin = regexpr('gsod95-current', web_content) +15
      End = regexpr('.txt', web_content) -1
      
      country_code = substr(web_content, Begin, End)
      country_code = unique(country_code)
      country_code = setNames(as.list(country_code), getCountryName() )
      country_code[order(names(sapply(country_code,'[[',1)))]
    })

    loadData <- reactive({
      link = paste0("http://academic.udayton.edu/kissock/http/Weather/gsod95-current/",input$citySelect, ".txt")
      data = read.table(link)
    }) 
    
    output$citySelector <- renderUI({
      selectInput("citySelect", "Choose city:", choices = getCountryCode())
    })
    
    output$table1 = renderDataTable(
      loadData()
    )
    
    output$timeSeries <- renderChart2({
      db <- loadData()
      db[db == -99] = NA
      
      for (i in 1:9) {
        db$V1[db$V1 == i] = paste0("0",i)
      }
      
      db$date = paste0(db$V3,"-",db$V1)
      db = aggregate(x = db$V4, by=list(db$date), FUN = mean, na.rm=TRUE)
      
      db = db[is.na(db$x) == FALSE,]
      
      if (input$celsOrFahr == "Celius") {
        db$x = (db$x - 32) / 1.8
      }
      
      p1 <- mPlot(x = 'Group.1', y = 'x', data = db, type = 'Line')
      p1$set(pointSize = 0, lineWidth = 2)
      return(p1)
    })
})
library(shiny)
library(rCharts)

shinyUI(fluidPage(
  titlePanel("Visualize temperature data directly from web"),
  uiOutput("citySelector"),
  radioButtons("celsOrFahr", "Celsius or Fahrenheit selector", choices =  c("Fahrenheit","Celius")),
  showOutput('timeSeries',lib='morris')
))
Code license: GPL-3