Skip to content

Instantly share code, notes, and snippets.

@tts
Last active December 31, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tts/7912178 to your computer and use it in GitHub Desktop.
Save tts/7912178 to your computer and use it in GitHub Desktop.
Shiny web app on data by the Helsinki Philharmonic Orchestra
###############################################################################################################
#
# Data is a subset of the original data file, and cleaned with OpenRefine
#
# Data: http://www.hri.fi/fi/data/helsingin-kaupunginorkesterin-konsertit-1882/
# Licence: http://www.hri.fi/lisenssit/hri-nimea/#english
#
# Blog posting: https://blogs.aalto.fi/suoritin/2013/12/12/data-from-the-helsinki-philharmonic-orchestra/
#
# 12.12.2013 Tuija Sonkkila
# 29.04.2015 New URL for the Shiny web app https://gist.github.com/tts/7912178
#
##############################################################################################################
library(shiny)
library(ggplot2)
library(plyr)
data <- read.table("data.tsv",
header = TRUE,
sep = "\t",
encoding = "UTF-8",
stringsAsFactors = FALSE)
names(data) <- c("id", "decade", "composer")
# Get rid of unnecessary (here) chars in strings
data$id <- sub("_.+", "", data$id)
data$decade <- substring(data$decade, 1, 3)
# Number of concerts per decade
concerts <- aggregate(data$id, by = list(Decade = data$decade), function(x) length(unique(x)))
# Clean composer data
data <- data[data$composer != "***",]
data <- data[data$composer != "",]
data <- data[data$composer != "väliaika",]
# How many works by a composer was played in various concerts?
playedByConcert <- count(data, c("decade", "id", "composer"))
playedByConcertOrder <- playedByConcert[order(playedByConcert$freq, decreasing = TRUE), c("id", "composer", "freq")]
# Number of works played by a composer during one concert
n <- 7
multi <- playedByConcertOrder[playedByConcertOrder$freq >= n,]
names(multi) <- c("Date", "Composer", "Number of works")
# http://stackoverflow.com/questions/8652674/r-xtable-and-dates
multi$Date <- as.character(as.Date(multi$Date, "%Y%m%d"))
# How many works by a composer was played in a decade?
playedByDecade <- count(data, c("decade", "composer"))
# Combined stats for calculating percentages
stats <- merge(playedByDecade, concerts, by.x = "decade", by.y = "Decade")
names(stats) <- c("Decade", "composer", "inConcert", "allConcerts")
# In how many concerts have composers been played in percentually by decade?
stats$percent <- round((stats$inConcert / stats$allConcerts) * 100, 2)
# How much composers to show to choose from = minimum number of works played in one decade
nc <- 50
############################################################################
#
# Data is a subset of the original data file, and cleaned with OpenRefine
#
# Data: http://www.hri.fi/fi/data/helsingin-kaupunginorkesterin-konsertit-1882/
# Licence: http://www.hri.fi/lisenssit/hri-nimea/#english
#
# 12.12.2013 Tuija Sonkkila
#
############################################################################
library(shiny)
library(ggplot2)
library(plyr)
shinyServer(function(input, output) {
cdata <- reactive({
if (is.null(input$c))
return(NULL)
stats[stats$composer %in% input$c,]
})
output$chart <- renderPlot({
if (is.null(cdata()))
return(NULL)
p <- ggplot(cdata(),
aes(x = paste(Decade, "*"), y = inConcert, group = composer)) +
geom_area(aes(fill = composer), color = 1) +
labs(x = "Decade", y = "Number of times played")
print(p)
})
output$chart2 <- renderPlot({
if (is.null(cdata()))
return(NULL)
p2 <- ggplot(data = cdata(),
aes(x = paste(Decade, "*"), y = percent, fill = composer)) +
geom_bar(stat = "identity", position = "dodge") +
labs(x = "Decade", y = "Percentage of all concerts")
print(p2)
})
output$data <- renderTable({
if (is.null(cdata()))
return(NULL)
cdata()[order(cdata()[c("composer")]),]
}, include.rownames = FALSE)
output$data2 <- renderTable({
if (is.null(cdata()))
return(NULL)
multi
}, include.rownames = FALSE)
})
############################################################################
#
# Data is a subset of the original data file, and cleaned with OpenRefine
#
# Data: http://www.hri.fi/fi/data/helsingin-kaupunginorkesterin-konsertit-1882/
# Licence: http://www.hri.fi/lisenssit/hri-nimea/#english
#
# 12.12.2013 Tuija Sonkkila
#
############################################################################
library(shiny)
library(ggplot2)
library(plyr)
shinyUI(pageWithSidebar(
headerPanel("Composers played in concerts organized by the Helsinki Philharmonic Orchestra since 1882"),
sidebarPanel(
h5(paste("At least ", nc, " concerts in one decade", sep = "")),
checkboxGroupInput(inputId = "c",
label = "Choose:",
choices = sort(unique(stats[stats$inConcert >= nc,]$composer)),
selected = c("Sibelius Jean", "Bach Johann Sebastian"))
),
mainPanel(
tabsetPanel(
tabPanel("Plot",
plotOutput("chart", height = "500px", width = "auto"),
br(),
plotOutput("chart2", height = "500px", width = "auto"),
br(),
a(href = "http://www.hri.fi/fi/data/helsingin-kaupunginorkesterin-konsertit-1882/", target = "_blank",
"Data | "),
a(href = "http://www.hri.fi/lisenssit/hri-nimea/#english", target = "_blank",
"Helsinki Region Infoshare - data pool licence")),
tabPanel("Plotted data",
tableOutput("data")),
tabPanel("Other stats",
h5(paste("Works played at least ", n, " times in one concert"), sep = ""),
br(),
tableOutput("data2"))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment