Created
December 28, 2013 03:19
-
-
Save andland/8155783 to your computer and use it in GitHub Desktop.
CD102.5 Top Songs by Artist in 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(XML) | |
library(lubridate) | |
library(sqldf) | |
library(reshape2) | |
library(ggplot2) | |
library(mgcv) | |
cat("loading old data...\n") | |
playlist=read.csv("CD101Playlist.csv",stringsAsFactors=FALSE) | |
colnames(playlist)[3]="Last Played" | |
max.date=max(mdy(substring(playlist[,3],nchar(playlist[,3])-9,nchar(playlist[,3])))) | |
cur.date=ymd(paste(today(tzone="EST"))) | |
if (max.date<=ymd(20131231) & max.date<cur.date) { | |
playlist.old=playlist | |
cat("updating data...\n") | |
startNum=0 | |
while(TRUE) { | |
theurl <- paste0("http://cd1025.com/about/playlists/now-playing/?start=",startNum) | |
table <- readHTMLTable(theurl,stringsAsFactors=FALSE)[[1]] | |
if (startNum==0) { | |
playlist=table[,-1] | |
} else { | |
playlist=rbind(playlist,table[,-1]) | |
} | |
dt=mdy(substring(table[1,4],nchar(table[1,4])-9,nchar(table[1,4]))) | |
print(dt) | |
if (dt<max.date) { | |
break | |
} | |
startNum=startNum+50 | |
} | |
playlist=unique(rbind(playlist,playlist.old)) | |
write.csv(playlist,"CD101Playlist.csv",row.names=FALSE) | |
} | |
playlist$Day=mdy(substring(playlist[,3],nchar(playlist[,3])-9,nchar(playlist[,3]))) | |
playlist$Time=hm(substring(playlist[,3],1,nchar(playlist[,3])-10)) | |
# playlist$Month=ymd(paste(year(playlist$Day),month(playlist$Day),"1",sep="-")) | |
playlist=playlist[order(playlist$Day,playlist$Time),] | |
playlist=subset(playlist,Day>=mdy("1/1/13") & Day<cur.date) | |
plays.per.day=sqldf("Select Day, Count(Artist) as Num | |
From playlist | |
Group By Day | |
Order by Day") | |
theurl <- "http://cd1025.com/about/playlists/now-playing/?start=0" | |
table <- readHTMLTable(theurl,stringsAsFactors=FALSE)[[1]] | |
recent.artists=head(table$Artist,5) | |
popular.artists=sqldf(paste0("Select Artist, Count(Artist) as [Plays This Year] | |
From playlist | |
Group By Artist | |
Order by [Plays This Year] DESC limit 5"))$Artist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyServer(function(input, output,session) { | |
output$playsPlot <- renderPlot({ | |
song.per.day=sqldf(paste0("Select Day, Song, Count(Song) as Num | |
From playlist | |
Where Artist='",toupper(input$artist),"' | |
Group By Day, Song | |
Order by Day, Song")) | |
dspd=dcast(song.per.day,Day~Song,sum,value.var="Num") | |
song.per.day=merge(plays.per.day[,1,drop=FALSE],dspd,all.x=TRUE) | |
song.per.day[is.na(song.per.day)]=0 | |
# which(apply(song.per.day[,-1],2,max)>=2) | |
# cbind(1:23,rank(-colSums(song.per.day[,-1])),colSums(song.per.day[,-1])) | |
if (ncol(song.per.day)>2) { | |
song.per.day=song.per.day[,c(1,which(rank(-colSums(song.per.day[,-1]))<=input$topSongs)+1)] | |
} | |
song.per.day=melt(song.per.day,1,variable.name="Song",value.name="Num") | |
song.per.day$Alpha=ifelse(song.per.day$Num>0,1,0) | |
p<-ggplot(song.per.day,aes(Day,Num,colour=Song))+geom_point(aes(alpha=Alpha))+ | |
geom_smooth(method="gam",family=poisson,formula=y~s(x),se=F,size=1)+ | |
labs(x="Date",y="Plays Per Day",title=input$artist,colour=NULL)+ | |
scale_alpha_continuous(guide=FALSE,range=c(0,0.5)) | |
print(p+theme_bw()) | |
},width=650,height=400) | |
output$view <- renderTable({ | |
sqldf(paste0("Select Song, Count(Song) as [Plays This Year] | |
From playlist | |
Where Artist='",toupper(input$artist),"' | |
Group By Song | |
Order by [Plays This Year] DESC")) | |
},include.rownames=FALSE) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyUI(pageWithSidebar( | |
# Application title | |
headerPanel("CD102.5 Playlist for 2013"), | |
sidebarPanel( | |
textInput("artist", "Artist",sample(recent.artists,1)), | |
sliderInput("topSongs", "Number of Songs to Plot", 1, 10, 3, step = 1, | |
round = FALSE, format = "#,##0.#####", locale = "us", | |
ticks = TRUE, animate = FALSE), | |
p(HTML(paste0("<br>Recent artist played include ",paste(recent.artists,collapse=", ")))), | |
p(HTML(paste0("The most popular artists this year are ",paste(popular.artists,collapse=", ")))), | |
p("My blog - ", | |
a("Statistically Significant", href="http://alandgraf.blogspot.com") | |
), | |
p("See other shiny apps I've built ", | |
a("here", href="http://andland.github.io/projects") | |
), | |
p("Follow me - ", | |
a("@andland", href="http://twitter.com/andland") | |
), | |
p("Data from ", | |
a("CD1025", href="http://cd1025.com/about/playlists/now-playing") | |
) | |
), | |
# Show a plot | |
mainPanel( | |
plotOutput("playsPlot"), | |
h3(HTML("<br>")), | |
tableOutput("view") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment