Instantly share code, notes, and snippets.

@tts /query.R
Last active Nov 22, 2015

Embed
What would you like to do?
Most discussed articles on diabetes in 2015
library(RCurl)
library(rAltmetric)
library(dplyr)
# Which articles on diabetes, published in 2015, are among the most discussed based on Altmetric.com data?
# See http://tuijasonkkila.fi/blog/2015/11/diabetes/
# http://www.fredtrotter.com/2014/11/14/hacking-on-the-pubmed-api/
baseurl <- "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmode=json&retmax=1000&term="
# Advanced search query details, http://www.ncbi.nlm.nih.gov/pubmed/advanced
advq <- '"diabetes mellitus"[MeSH Terms] AND ("2015/01/01"[PDAT] : "3000"[PDAT])'
advq_encoded <- URLencode(advq)
q <- paste0(baseurl,advq_encoded)
res <- getURLContent(q)
res_parsed <- jsonlite::fromJSON(res)
ids <- res_parsed$esearchresult$idlist
ids2 <- paste("pmid/", ids, sep = "")
# Query Altmetric API
raw_metrics <- llply(ids2, altmetrics, .progress = 'text')
metric_data <- ldply(raw_metrics, altmetric_data)
# Sort by Altmetric score
sorted_by_score <- metric_data[with(metric_data, order(score, decreasing=T)), ]
# Filter columns
topscore <- sorted_by_score %>%
select (title,url,score,details_url)
write.csv(topscore, file = "diabetes2015_topAltmetricScore.csv", row.names = F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment