Skip to content

Instantly share code, notes, and snippets.

@jamesthomson
Last active August 29, 2015 14:10
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 jamesthomson/9f0d4dc67dd1884c7dd2 to your computer and use it in GitHub Desktop.
Save jamesthomson/9f0d4dc67dd1884c7dd2 to your computer and use it in GitHub Desktop.
#server.r
library(shiny)
shinyServer(function(input, output) {
library(rjson)
library(ggplot2)
library(grid)
#create functions to pull down data from api
topArtists<-function(limit=100){
eval(parse(text=paste0("search<-fromJSON(file=\"http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&format=json&limit=", limit, "\")")))
topArtists<-data.frame(position=NULL, artist=NULL, playcount=NULL, listeners=NULL)
for (i in c(1:limit)){
temp<-data.frame(
position=i,
artist=search$artist$artist[[i]]$name,
playcount=search$artist$artist[[i]]$playcount,
listeners=search$artist$artist[[i]]$listeners
)
topArtists<-rbind(topArtists, temp)
}
return(topArtists)
}
topTracks<-function(limit=5){
eval(parse(text=paste0("search<-fromJSON(file=\"http://ws.audioscrobbler.com/2.0/?method=chart.gettoptracks&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&format=json&limit=", limit, "\")")))
topTracks<-data.frame(position=NULL, track=NULL, artist=NULL, playcount=NULL, listeners=NULL, duration=NULL)
for (i in c(1:limit)){
temp<-data.frame(
position=i,
track=search$tracks$track[[i]]$name,
artist=search$tracks$track[[i]]$artist$name,
playcount=search$tracks$track[[i]]$playcount,
listeners=search$tracks$track[[i]]$listeners,
duration=search$tracks$track[[i]]$duration
)
topTracks<-rbind(topTracks, temp)
}
return(topTracks)
}
#run functions to pull data
topArtistsData<-topArtists(100)
topTracksData<-topTracks(100)
#produce table and plots for top artists
output$artistData<-renderTable({
topArtistsData[1:input$obs,2:4]
})
output$artistListeners<-renderPlot({
data<-topArtistsData[1:input$obs,2:4]
ggplot(data, aes(x=artist, y=as.numeric(as.character(listeners)))) +
geom_bar(stat="identity", fill="red", colour="black") +
labs(title="Listeners", y="", x="") +
theme(title=element_text(size=20)) +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5, size=21-(0.1*input$obs)), axis.ticks.margin=unit(0,'cm')) +
theme(axis.text.y=element_text(size=15)) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill="white"))
})
output$artistPlaycount<-renderPlot({
data<-topArtistsData[1:input$obs,2:4]
ggplot(data, aes(x=artist, y=as.numeric(as.character(playcount)))) +
geom_bar(stat="identity", fill="red", colour="black") +
labs(title="Plays", y="", x="") +
theme(title=element_text(size=20)) +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5, size=21-(0.1*input$obs)), axis.ticks.margin=unit(0,'cm')) +
theme(axis.text.y=element_text(size=15)) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill="white"))
})
#produce table and plots for top tracks
output$trackData<-renderTable({
topTracksData[1:input$obs,2:5]
})
output$trackListeners<-renderPlot({
data<-topTracksData[1:input$obs,2:5]
ggplot(data, aes(x=track, y=as.numeric(as.character(listeners)))) +
geom_bar(stat="identity", fill="red", colour="black") +
labs(title="Listeners", y="", x="") +
theme(title=element_text(size=20)) +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5, size=15-(0.1*input$obs)), axis.ticks.margin=unit(0,'cm')) +
theme(axis.text.y=element_text(size=15)) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill="white"))
})
output$trackPlaycount<-renderPlot({
data<-topTracksData[1:input$obs,2:5]
ggplot(data, aes(x=track, y=as.numeric(as.character(playcount)))) +
geom_bar(stat="identity", fill="red", colour="black") +
labs(title="Plays", y="", x="") +
theme(title=element_text(size=20)) +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5, size=15-(0.1*input$obs)), axis.ticks.margin=unit(0,'cm')) +
theme(axis.text.y=element_text(size=15)) +
theme(panel.grid = element_blank(), panel.background = element_rect(fill="white"))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment