Skip to content

Commit

Permalink
#44 fixes to orcid_works function, and concomitant fixes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Mar 16, 2018
1 parent 6184a44 commit 58d1141
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion 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.0.9442
Version: 0.4.0.9444
Authors@R: c(person("Scott", "Chamberlain", role = c("aut", "cre"),
email = "myrmecocystus@gmail.com",
comment = c(ORCID = "0000-0003-1444-9135")))
Expand Down
1 change: 0 additions & 1 deletion R/identifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ identifiers.list <- function(x, type = "doi", ...) {
#' @export
#' @rdname identifiers
identifiers.orcid_id <- function(x, type = "doi", ...) {
# prof <- attr(x, "profile")
wks <- works(x)
tmp <- wks$`external-ids.external-id`
unlist(lapply(tmp, function(z) {
Expand Down
51 changes: 35 additions & 16 deletions R/orcid_works.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
#' # get all works
#' res <- orcid_works(orcid = "0000-0002-9341-7985")
#' res$`0000-0002-9341-7985`
#' res$`0000-0002-9341-7985`$group
#' res$`0000-0002-9341-7985`$group$`work-summary`
#' res$`0000-0002-9341-7985`$group$`work-summary`[[1]]
#' str(res$`0000-0002-9341-7985`$group$`work-summary`[[1]])
#' res$`0000-0002-9341-7985`$type
#' str(res$`0000-0002-9341-7985`)
#'
#' # get individual works
#' orcid_works(orcid = "0000-0002-9341-7985", 5011717)
#' orcid_works(orcid = "0000-0002-9341-7985", put_code = 5011717)
#' orcid_works(orcid = "0000-0002-9341-7985", put_code = c(5011717, 15536016))
#'
#' # change formats
Expand All @@ -37,17 +35,15 @@
#' # get citations
#' id <- "0000-0001-7678-8656"
#' x <- orcid_works(id)
#' vapply(x[[1]]$group$`work-summary`, function(z) {
#' orcid_works(id, put_code = z$`put-code`)[[1]]$citation$`citation-value`
#' }, "")
#' orcid_works(id, put_code = x[[1]]$`put-code`)[[1]]$`work.citation.citation-value`
#'
#' ## or send many put codes at once, will be split into chunks of 50 each
#' id <- "0000-0001-6758-5101"
#' x <- orcid_works(id)
#' pcodes <- unlist(lapply(x[[1]]$group$`work-summary`, "[[", "put-code"))
#' pcodes <- x[[1]]$`put-code`
#' length(pcodes)
#' res <- orcid_works(id, put_code = pcodes)
#' tibble::as_tibble(data.table::setDF(data.table::rbindlist(res, use.names = TRUE, fill = TRUE)))
#' res <- orcid_works(orcid = id, put_code = pcodes)
#' lapply(res$`0000-0001-6758-5101`, head, n = 1)
#' }
orcid_works <- function(orcid, put_code = NULL, format = "application/json",
...) {
Expand All @@ -57,6 +53,7 @@ orcid_works <- function(orcid, put_code = NULL, format = "application/json",
stop("if 'put_code' is given, 'orcid' must be length 1")
}
}

pth <- if (is.null(put_code)) {
"works"
} else {
Expand All @@ -67,13 +64,35 @@ orcid_works <- function(orcid, put_code = NULL, format = "application/json",
file.path("work", put_code)
}
}

if (length(pth) > 1) {
tmp <- Map(function(z) orcid_prof_helper(orcid, z, ctype = format), pth)
unname(lapply(tmp, function(z) z$bulk))
tt <- unname(lapply(tmp, function(z) z$bulk))
stats::setNames(list(tt), orcid)
} else {
stats::setNames(
lapply(orcid, orcid_prof_helper, path = pth, ctype = format, ...),
orcid
)
tmp <- lapply(orcid, orcid_prof_helper, path = pth, ctype = format, ...)
tt <- lapply(tmp, function(z) {
if (grepl("works", pth) || (is.list(pth) && grepl("works/", pth[[1]]))) {
if (is.null(put_code)) {
bb <- z$group$`work-summary`
if (is_json(format)) list(as_dt(bb, FALSE)) else list(bb)
} else {
list(z$bulk)
}
} else if (grepl("work/", pth)) {
if (is_json(format)) {
jsonlite::fromJSON(
jsonlite::toJSON(list(z, z), auto_unbox=TRUE)
)[1,]
} else {
z
}
} else {
z$bulk
}
})
stats::setNames(tt, orcid)
}
}

is_json <- function(format) grepl("json", format)
4 changes: 2 additions & 2 deletions R/works.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
works <- function(x) {
tmp <- as.orcid(x)
works <- orcid_works(tmp[[1]]$name$path)
if (is.null(works) || NROW(works[[1]]$group) == 0) {
if (is.null(works) || NROW(works[[1]][[1]]) == 0) {
dat <- tibble::data_frame()
structure(dat, class = c(class(dat), "works"),
orcid = names(tmp))
} else {
dat <- as_dt(works[[1]]$group$`work-summary`)
dat <- tibble::as_tibble(works[[1]][[1]])
structure(dat, class = c(class(dat), "works"),
orcid = names(tmp))
}
Expand Down
8 changes: 5 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ orcid_putcode_helper <- function(path, orcid, put_code, format, ...) {
}
}

as_dt <- function(x) {
tibble::as_tibble(data.table::setDF(
as_dt <- function(x, tibble = TRUE) {
z <- data.table::setDF(
data.table::rbindlist(x, use.names = TRUE, fill = TRUE)
))
)
if (tibble) z <- tibble::as_tibble(z)
return(z)
}
18 changes: 7 additions & 11 deletions man/orcid_works.Rd

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

0 comments on commit 58d1141

Please sign in to comment.