Creating a timeline using R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
On Jan, 10th David Bowie left this earthly realm. That day, as I do like his music a lot, I decided to create a timeline and recently I decided to make the code publicly available.
Required packages
You need rCharts whose author is Ramnath Vaidyanathan. The package does not contain an extensive documentation yet but is a great tool.
CSV file
Create a CSV file whose basic columns are
- Start Date
- End Date
- Headline
- Text
- Media
- Media Credit
- Media Caption
- Media Thumbnail
- Type
- Tag
The only tricky part is that in Excel, Google Sheets or any other cellsheet software both start and end date must be formatted as m/d/yy hh:mm.
Loading packages
setwd("/Users/pacha/bowie-timeline/") #change this to your working folder library(plyr) library(RCurl) library(rCharts)
If rChart is not installed it can be installed using devtools. The package website shows some deprecated parameters and this is what works.
require(devtools) install_github('ramnathv/rCharts')
Reading data
discography <- read.csv("discography.csv", as.is = TRUE) discography <- alply(discography, 1, function(x) { list(startDate = x$Start.Date, headline = x$Headline, text = x$Text, asset = list(media = x$Media)) })
Creating timeline
tl = Timeline$new() tl$main(headline = paste0("David Bowie"), type = "default", text = paste0("Discography ", format(as.Date("1967-01-01"), "%Y"), "-", format(as.Date("2016-01-01"), "%Y")), startDate = format(as.Date("1967-01-01"), "%Y"), asset = list(media = "img/cover.jpg")) names(discography) <- NULL tl$event(discography) tl$save("index.html")
Adjustments
Update: Daisuke Ichikawa's blog has a solution to change the JS path without editing the final html outside RStudio. This are the steps, do them or the timeline won't work in other computer.
js <- paste(readLines("index.html", warn = F), collapse = "\n") js <- gsub("/Users/pacha/bowie-timeline/timeline/js/storyjs-embed.js", "timeline/js/storyjs-embed.js", js) writeLines(js, con = "index.html")
Now, if you open your timeline the dates are displayes as dd/mm/yyyy hh:mm, if you want to show years only then open the html in your editor, look for startDate
and the dates will be shown as "1/1/1967 00:00", you can change it to "1967" without further steps but do the same to any startDate
(e.g. use replace "1/1/" for "" in your editor).
Optional step
The final step is to go to the timeline folder that you just copied and get hands into the CSS file, then add the Creative Commons button and favicon, and then you will obtain exactly the same result as me. You can also add Google Analytics code to your timeline as with any html file.
Verification
To avoid many commits to Github, you can run this from terminal
cd /Users/pacha/bowie-timeline python -m SimpleHTTPServer 8000
and then in your brower go to http://localhost:8000/ to see your changes.
See the full project on Github
Go to the timeline repo available here.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.