Instantly share code, notes, and snippets.

@tts /global.R
Last active Aug 29, 2015

Embed
What would you like to do?
Snow depth data by FMI as a Shiny application
###################################################################
#
# Data by Finnish Meteorological Institute
# http://en.ilmatieteenlaitos.fi/open-data-licence
#
# rmi R package Jussi Jousimo et al. (C) 2014
# https://github.com/rOpenGov/fmi
#
# Blog post http://tuijasonkkila.fi/blog/2015/01/snow-in-lapland/
#
####################################################################
library(shiny)
library(xts)
library(dygraphs)
library(leaflet)
library(shinythemes)
# ogr2ogr -f "GeoJSON" hiihtonnnn.gpx hiihtonnnn.geojson tracks
hiihto1213 <- RJSONIO::fromJSON("hiihto1213.geojson")
hiihto1214 <- RJSONIO::fromJSON("hiihto1214.geojson")
hiihto1215 <- RJSONIO::fromJSON("hiihto1215.geojson")
hiihto1216 <- RJSONIO::fromJSON("hiihto1216.geojson")
saariselka <- read.table(file = "snow.Saariselka",
header = TRUE,
sep = ";",
colClasses = c("Date", "numeric"),
stringsAsFactors = FALSE)
kilpisjarvi <- read.table(file = "snow.Kilpisjarvi",
header = TRUE,
sep = ";",
colClasses = c("Date", "numeric"),
stringsAsFactors = FALSE)
salla <- read.table(file = "snow.Salla",
header = TRUE,
sep = ";",
colClasses = c("Date", "numeric"),
stringsAsFactors = FALSE)
# Assuming here that -1 = 0
saariselka$Snow[saariselka$Snow == -1] <- 0
kilpisjarvi$Snow[kilpisjarvi$Snow == -1] <- 0
salla$Snow[salla$Snow == -1] <- 0
saariselka.xts <- as.xts(as.matrix(saariselka), order.by=saariselka[,1])
kilpisjarvi.xts <- as.xts(as.matrix(kilpisjarvi), order.by=kilpisjarvi[,1])
salla.xts <- as.xts(as.matrix(salla), order.by=salla[,1])
s.k <- cbind(saariselka.xts, kilpisjarvi.xts)
snow <- cbind(s.k, salla.xts)
shinyServer(function(input, output, session) {
output$leaflet <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPopups(20.79, 69.05, "Enontekio, Kilpisjarvi") %>%
addPopups(27.41, 68.42, "Inari, Saariselka tourist centre") %>%
addPopups(29.18, 67.16, "Salla, Naruska") %>%
setView(18.5167, 67.9000, zoom = 6) %>%
addGeoJSON(hiihto1213) %>%
addGeoJSON(hiihto1214) %>%
addGeoJSON(hiihto1215) %>%
addGeoJSON(hiihto1216)
})
dayPicked <- reactive({
input$day
})
output$dygraph <- renderDygraph({
dygraph(snow) %>%
dySeries("Snow", label = "Saariselka") %>%
dySeries("Snow.1", label = "Kilpisjarvi") %>%
dySeries("Snow.2", label = "Salla") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyAxis("y", label = "cm") %>%
dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2")) %>%
dyLegend(show = "onmouseover", width = 400, showZeroValues = FALSE, hideOnMouseOut = TRUE) %>%
dyEvent(date = dayPicked(), label = as.character(dayPicked()), labelLoc = "bottom")
})
})
shinyUI(fluidPage(
theme = shinytheme("cosmo"),
titlePanel("Snow Depth 2012-2014"),
sidebarLayout(
sidebarPanel(
br(),
dateInput("day",
label = "Pick a day:",
min = "2011-09-01",
max = "2014-12-31",
value = "2014-11-01",
startview = "year"),
br(),
p("Draw range to zoom in, double-click to zoom out again."),
br(),
br(),
br(),
HTML("Data by <a href='http://en.ilmatieteenlaitos.fi/open-data-licence'>
Finnish Meteorological Institute</a>."),
br(),
HTML("<a href='http://www.github.com/rOpenGov/fmi'>rmi R package</a>
Jussi Jousimo et al. (C) 2014."),
br(),
br(),
br(),
HTML("More on this graph, see <a href='http://tuijasonkkila.fi/blog/2015/01/snow-in-lapland/'>blog posting</a>.")
),
mainPanel(
tabsetPanel(
tabPanel("Graph", dygraphOutput("dygraph")
),
tabPanel("Map", leafletOutput("leaflet"))
)
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment