Wednesday, 20 May 2015

Put Google Scholar citations on your personal website with R, scholar, ggplot2 and cron 20.5.15


I have been looking for a solution to put my Google Scholar citations on my personal website for quite some time now. Some apps/gadgets seem to have existed to do so (e.g. citations-gadget), but none seem to be functioning today because of changes at Google Scholar, and embedding your entire profile page won't work either. Basically, I had given up my search for a solution, until a few months ago, I discovered a blog post on the R scholar package by James Keirstead (development is taking place on github).
For installing the R scholar package (and its dependencies) in Linux, it is possible you need to install the following packages first (I was running Linux Lite 2.0 on my personal laptop and had to do so):
sudo apt-get install libcurl4-openssl-dev libxml2-dev
Then install the R package using the conventional
install.packages('scholar')
in R. After loading the package, you can get the citation history from a Google Scholar profile page (citations per year, as displayed in the bar plot on the right-hand side of the page) using a single function call on the Google Scholar profile ID. Getting the data, and making a plot with the ggplot2 package, which contains a timestamp, is done as follows:
  library(scholar)
  library(ggplot2)
  cit <- get_citation_history('u421gfwAAAAJ')
  png('scholar_citations_u421gfwAAAAJ.png',width=800,height=300,res=150)
  ggplot(cit,aes(x=year,y=cites))+
    geom_bar(stat='identity')+
    theme_bw()+
    xlab('Year of citation')+
    ylab('Google Scholar\n cites')+
    annotate('text',label=format(Sys.time(), "%Y-%m-%d %H:%M:%S %Z"),x=-Inf,y=Inf,vjust=1.5,hjust=-0.05,size=3,colour='gray')
  dev.off()
With my own humble contribution to science, the resulting image is something like this:


Now, for the automation of this image creation task, you can save the above code as an R script, and simply use cron in Linux. To set up your task with crontab, use
crontab -e
in the terminal and add a single line of text with the instructions to run your script. If you want to execute the script hourly, all you have to do is add a line like this:
0 * * * * cd /home/your/folder && Rscript your_script.R
and save the file. If you do this in a publicly accessible folder, or you can make the image itself publicly accessible (options to do so are Google Drive, Ubuntu One, Dropbox, etc.), you can easily embed it on your personal website with an <img> HTML tag. I currently have it set up like this, so it updates every hour when my personal laptop is switched on. The up-to-date version of the plot above should be online here.

Not so hard after all! :)
5 Bart Rogiers: Put Google Scholar citations on your personal website with R, scholar, ggplot2 and cron I have been looking for a solution to put my Google Scholar citations on my personal website for quite some time now. Some apps/gadg...

6 comments:

  1. I've not seen that positioning trick before in annotate (x=-Inf, y=Inf) — looks like that gives you automatic top-left positioning without affecting axis limits? Neat!

    ReplyDelete
    Replies
    1. Yes, that's exactly what it does. Since I know about it, I use it all the time!

      Delete
  2. I had a wee squiz around your blog and see you`re interested in the similar things to me, like art, painting, etc and as an R novice, I found your post more interesting because of the insight your post gave me about 'how you think'. It`s nice to know the person behind the code. Anyway, I`m trying to use some basic gglot2 functions today, and will return to this page once I know more. Thanks.

    ReplyDelete
    Replies
    1. Hi there Robert! I'm glad you found some interesting things here! Good luck with ggplot2! I'm using it on a daily basis now. It is definitely worth every hour you spend learning to use it!

      Delete

< >