diff --git a/DESCRIPTION b/DESCRIPTION index 7278d9e..3158d83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Title: Interface to the 'Orcid.org' 'API' Description: Client for the 'Orcid.org' 'API' (). 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"))) diff --git a/R/identifiers.R b/R/identifiers.R index 9fd9a07..0ac34f5 100644 --- a/R/identifiers.R +++ b/R/identifiers.R @@ -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) { diff --git a/R/orcid_works.R b/R/orcid_works.R index 0f63f6e..f47f888 100644 --- a/R/orcid_works.R +++ b/R/orcid_works.R @@ -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 @@ -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", ...) { @@ -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 { @@ -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) diff --git a/R/works.R b/R/works.R index 56196d9..44cc6ca 100644 --- a/R/works.R +++ b/R/works.R @@ -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)) } diff --git a/R/zzz.R b/R/zzz.R index 3b59b23..a7c89fb 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -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) } diff --git a/man/orcid_works.Rd b/man/orcid_works.Rd index 73b3e63..ca2b0b9 100644 --- a/man/orcid_works.Rd +++ b/man/orcid_works.Rd @@ -37,13 +37,11 @@ there's an element returned for each ORCID you put in. # 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 @@ -57,16 +55,14 @@ orcid_works("0000-0002-9341-7985", 5011717, # 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) } }