Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

orcid_citations fxn added #69

Merged
merged 15 commits into from
May 31, 2019
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
.Rproj.user
inst/ignore/auth.R
inst/ignore/issn_title_collect.R
.httr-oauth
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ language: r
sudo: false
cache: packages

addons:
apt:
packages:
- libv8-dev

matrix:
include:
- os: linux
Expand Down
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Title: Interface to the 'Orcid.org' API
Description: Client for the 'Orcid.org' API (<https://orcid.org/>).
Functions included for searching for people, searching by 'DOI',
and searching by 'Orcid' 'ID'.
Version: 0.4.3.9110
Version: 0.4.3.9113
Authors@R:
c(person(given = "Scott",
family = "Chamberlain",
Expand All @@ -30,7 +30,9 @@ Imports:
data.table
Suggests:
testthat,
knitr
knitr,
rcrossref,
handlr
RoxygenNote: 6.1.1
VignetteBuilder: knitr
X-schema.org-applicationCategory: Literature
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(orcid_activities)
export(orcid_address)
export(orcid_auth)
export(orcid_bio)
export(orcid_citations)
export(orcid_doi)
export(orcid_educations)
export(orcid_email)
Expand All @@ -40,6 +41,7 @@ export(works)
importFrom(crul,HttpClient)
importFrom(fauxpas,find_error_class)
importFrom(fauxpas,http)
importFrom(handlr,HandlrClient)
importFrom(httr,add_headers)
importFrom(httr,oauth2.0_token)
importFrom(httr,oauth_app)
Expand Down
169 changes: 169 additions & 0 deletions R/orcid_citations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#' Get citations
#'
#' @export
#' @param orcid (character) Orcid identifier(s) of the form
#' XXXX-XXXX-XXXX-XXXX. required.
#' @param put_code (character/integer) one or more put codes. up to
#' 50. optional
#' @param cr_format Used in Crossref queries only. Name of the format. One of
#' "rdf-xml", "turtle",
#' "citeproc-json", "citeproc-json-ish", "text", "ris", "bibtex" (default),
#' "crossref-xml", "datacite-xml","bibentry", or "crossref-tdm". The format
#' "citeproc-json-ish" is a format that is not quite proper citeproc-json.
#' passed to `rcrossref::cr_cn`. The special "citeproc2bibtex" value asks
#' for citeproc-json from Crossref, then converts it into bibtex format
#' using [handlr::HandlrClient]
#' @param cr_style Used in Crossref queries only. A CSL style (for text
#' format only). See ‘get_styles()’ for options. Default: apa.
#' passed to `rcrossref::cr_cn`
#' @param cr_locale Used in Crossref queries only. Language locale.
#' See [Sys.getlocale], passed to `rcrossref::cr_cn`
#' @param ... Curl options passed on to [crul::HttpClient]
#' @template deets
#' @details This function is focused on getting citations only.
#' You can get all citations for an ORCID, or for certain works
#' using a PUT code, or for many PUT codes.
#'
#' We attempt to get citations via Crossref using \pkg{rcrossref}
#' whenever possible as they are the most flexible and don't have as
#' many mistakes in the text. If there is no DOI, we fetch the
#' citation from ORCID.
#'
#' Right now we get JSON citations back. We'd like to support bibtex
#' format. DOI.org supports this but not ORCID.
#'
#' @return data.frame, with the columns:
#'
#' - put: ORCID PUT code, identifying the work identifier in ORCID's records
#' - id: the external identifier
#' - id_type: the type of external identifier
#' - format: the citation format retrieved
#' - citation: the citation as JSON
#'
#' @examples \dontrun{
#' (res <- orcid_citations(orcid = "0000-0002-9341-7985"))
#' (res2 <- orcid_citations(orcid = "0000-0002-1642-628X"))
#' (res2 <- orcid_citations(orcid = c("0000-0002-9341-7985", "0000-0002-1642-628X")))
#'
#' # get individual works
#' ## a single put code
#' (a <- orcid_citations(orcid = "0000-0002-9341-7985", put_code = 5011717))
#' ## many put codes
#' (b <- orcid_citations(orcid = "0000-0002-9341-7985",
#' put_code = c(5011717, 15536016)))
#'
#' # request other formats, Crossref only
#' orcid_citations(orcid = "0000-0002-9341-7985", cr_format = "turtle")
#'
#' # parse citation data if you wish
#' # for parsing bibtex can use bibtex package or others
#' (res <- orcid_citations(orcid = "0000-0002-9341-7985"))
#' lapply(res[res$format == "csl-json", "citation"][[1]], jsonlite::fromJSON)
#'
#' # lots of citations
#' orcid_citations(orcid = "0000-0001-8642-6325")
#'
#' # example with no external identifier, returns NA's
#' orcid_citations(orcid = "0000-0001-8642-6325", 26222265)
#' }
orcid_citations <- function(orcid, put_code = NULL, cr_format = "bibtex",
cr_style = "apa", cr_locale = "en-US", ...) {

if (!is.null(put_code)) {
if (length(orcid) > 1) {
stop("if 'put_code' is given, 'orcid' must be length 1")
}
}

tmp <- orcid_works(orcid, put_code)

dat <- if (!is.null(put_code)) {
list(list(tmp[[1]]$works))
} else {
lapply(tmp, function(w) split(w$works, w$works$`put-code`))
}

if (length(orcid) > 1) {
Map(function(a, b) each_orcid(a, b, put_code, cr_format, cr_style, cr_locale),
dat, orcid, ...)
} else {
# each_orcid(dat[[1]], orcid, put_code, cr_format, cr_style, cr_locale, ...)
do_all(dat[[1]], orcid, put_code, cr_format, cr_style, cr_locale)
}
}

each_orcid <- function(m, orcid, put_code, cr_format, cr_style, cr_locale, ...) {
cites <- plyr::llply(m, function(z) {
# fix for whenever > 1 put code to make column names more useable
if (all(grepl("work", names(z)))) {
names(z) <- gsub("^work\\.", "", names(z))
}
pc <- z$`put-code`
if (!is.null(put_code)) {
if (length(put_code) == 1) {
df <- z$`external-ids`$`external-id`[[1]]
process_cites(df, pc, orcid, cr_format, cr_style, cr_locale, ...)
} else {
df <- z$`external-ids`
Map(process_cites, df, pc, orcid = orcid, cr_format = cr_format,
cr_style = cr_style, cr_locale = cr_locale, ...)
}
} else {
df <- z$`external-ids.external-id`[[1]]
process_cites(df, pc, orcid, cr_format, cr_style, cr_locale, ...)
}
}, .inform = TRUE)
# unnest if no names at top level
if (is.null(names(cites[[1]])) && length(cites[[1]]) > 1) cites <- unlist(cites, FALSE)
# combine
as_dt(cites)
}

process_cites <- function(df, pc, orcid, cr_format, cr_style, cr_locale, ...) {
if (length(df) == 0) {
return(list(put = pc, id = NA_character_, id_type = NA_character_,
format = NA_character_, citation = ""))
}
if ("doi" %in% df$`external-id-type`) {
id <- df[df$`external-id-type` %in% "doi", "external-id-value"]
type <- "doi"
if (cr_format == "citeproc2bibtex") {
chkpkg('handlr')
cr_format = "citeproc-json"
fmat <- "bibtex"
ct <- cite_doi(id, cr_format, cr_style, cr_locale, ...) %||% ""
cli <- handlr::HandlrClient$new(x = ct)
cli$read("citeproc")
ct <- paste0(cli$write("bibtex"), collapse = "\n")
} else {
fmat <- cr_format
ct <- cite_doi(id, cr_format, cr_style, cr_locale, ...) %||% ""
}
} else {
id <- df[df$`external-id-type` %in% "eid", "external-id-value"]
type <- "eid"
fmat <- 'csl-json'
ct <- cite_put(orcid, pc, ...) %||% ""
}
list(put = pc, id = id, id_type = type, format = fmat, citation = ct)
}

chkpkg <- function(x) {
if (!requireNamespace(x, quietly = TRUE)) {
stop("Please install ", x, call. = FALSE)
} else {
invisible(TRUE)
}
}

cite_doi <- function(x, cr_format = "bibtex", cr_style = "apa", cr_locale = "en-US", ...) {
chkpkg('rcrossref')
rcrossref::cr_cn(x, format = cr_format, style = cr_style, locale = cr_locale, raw = TRUE, ...)
}

cite_put <- function(orcid, pc, ...) {
orcid_prof_helper(orcid, file.path("work", pc),
ctype = "application/vnd.citationstyles.csl+json",
# ctype = "application/x-bibtex",
parse = FALSE, ...)
}
8 changes: 8 additions & 0 deletions R/rorcid-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#' @importFrom crul HttpClient
#' @importFrom fauxpas http find_error_class
#' @importFrom jsonlite fromJSON
#' @importFrom handlr HandlrClient
#' @name rorcid-package
#' @docType package
#' @author Scott Chamberlain \email{myrmecocystus@@gmail.com}
Expand All @@ -66,6 +67,13 @@ NULL

#' Lookup vector for journal titles by ISSN
#'
#' named vector of journal titles. the values are journal titles and
#' the names are ISSN's.
#'
#' length: 57,968
#'
#' data collected on 2018-06-13 from Crossref
#'
#' @name issn_title
#' @docType data
#' @keywords data
Expand Down
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ pluck <- function(x, name, type) {

pop <- function(x, name) x[ !names(x) %in% name ]

orcid_prof_helper <- function(x, path, ctype = ojson, ...) {
orcid_prof_helper <- function(x, path, ctype = ojson, parse = TRUE, ...) {
url2 <- file.path(orcid_base(), x, path)
out <- orc_GET(url2, ctype = ctype, ...)
switch_parser(ctype, out)
if (parse) switch_parser(ctype, out) else out
}

switch_parser <- function(ctype, x) {
Expand Down
8 changes: 7 additions & 1 deletion man/issn_title.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions man/orcid_citations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.