Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created February 25, 2013 23:17
Show Gist options
  • Save atbradley/5034250 to your computer and use it in GitHub Desktop.
Save atbradley/5034250 to your computer and use it in GitHub Desktop.
Setup to analyze playlist data from Radio Paradise, and an HTML chart function adapted from Matt Asher
songs <- read.table('http://data.adamtbradley.com/RadioParadisePlays.txt',
sep='-', skip=1, quote='', strip.white=T,
allowEscapes=T, col.names=c('time', 'artist', 'track'))
songs$artist <- sub('&ndash;', '-', songs$artist)
songs$track <- sub('&ndash;', '-', songs$track)
summ <- table(paste(songs$artist, songs$track, sep=": "))
summ.artists = table(songs$artist)
smry <- data.frame(summ)
colnames(smry) <- c('title', 'freq')
smry.artists <- data.frame(summ.artists)
colnames(smry.artists) <- c('title', 'freq')
# Created by Matt Asher for statisticsblog.com
# Feel free to share and modify so long as this header remains
# Adapted from http://tinyurl.com/abvt7el
HTML.chart <- function(x, maxRows=F, maxBarWidth=50,
tableStyle='', ytLinks=F) {
pTable <- x[order(x[2], decreasing=T),]
maxPlayed <- max(pTable[,2])
toWrite <- '<table border=0 cellspacing=0 cellpadding=5>'
if ( !maxRows ) maxRows <- dim(pTable[1])
else maxRows <- min(maxRows, dim(pTable)[1])
for(m in 1:maxRows) {
toWrite = paste(toWrite, '<tr>')
toWrite = paste(toWrite, '<td align="right"><img src="http://data.adamtbradley.com/red.png" title="Played ', pTable[m,2], ' times" height=14 width=', round( (maxBarWidth * pTable[m,2])/maxPlayed), '></td>', sep="")
toWrite = paste(toWrite, '<td>&nbsp;&nbsp;</td>')
toWrite = paste(toWrite, '<td>')
if ( ytLinks ) toWrite = paste(toWrite, '<a href="http://www.google.com/search?q=', URLencode(paste('youtube', pTable[m,1] , sep=" ")) ,'&btnI" target="_new" title="Watch video">', sep="")
toWrite = paste(toWrite, pTable[m,1], "\n")
if ( ytLinks ) toWrite = paste(toWrite, '</a>')
toWrite = paste(toWrite, '</td><tr>')
}
toWrite <- paste(toWrite, '</table>')
toWrite
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment