Skip to content

Instantly share code, notes, and snippets.

@karthik
Created August 12, 2017 00:04
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 karthik/c1ab47b1c8d472de96595327028cbdf1 to your computer and use it in GitHub Desktop.
Save karthik/c1ab47b1c8d472de96595327028cbdf1 to your computer and use it in GitHub Desktop.
library(rAltmetric)
library(purrr)
# we launch purrr because it gives us the safely function
ids <- c("10.1371/journal.pbio.0000002",
"10.1371/journal.pbio.0000003",
"10.1371/journal.pbio.0000004",
"10.1371/journal.pbio.0000005",
"10.1371/journal.pbio.0000006",
"10.1371/journal.pbio.0000007",
"10.1371/journal.pbio.0000008",
"10.1371/journal.pbio.0000009"
)
ids2 <- as.list(ids)
safe_metrics <- safely(altmetrics)
metric_data <- function(x) {
alm <- safe_metrics(doi = x)
# Safely ensures that the function returns NULL if nothing else comes out
if(is.null(alm$result)) {
NULL
} else {
data.frame(t(unlist(alm$result)), stringsAsFactors = FALSE)
}
}
results <- lapply(ids2, metric_data)
# This next bit removes NULL (ie. dois for which there were no metrics)
results <- Filter(Negate(is.null), results)
# Now this is a list of lists. You can flatten now as you please
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment