diff --git a/R/oa2df.R b/R/oa2df.R index feb57ca..b17eca3 100644 --- a/R/oa2df.R +++ b/R/oa2df.R @@ -8,6 +8,10 @@ #' The argument can be one of c("works", "authors", "venues", "institutions", "concepts"). #' @param abstract Logical. If TRUE, the function returns also the abstract of each item. #' Ignored if entity is different from "works". Defaults to TRUE. +#' @param verbose Logical. +#' If TRUE, print information about the dataframe conversion process. +#' Defaults to TRUE. +#' #' @inheritParams oa_query #' @inheritParams oa_request #' @return A tibble/dataframe result of the original OpenAlex result list. @@ -187,10 +191,10 @@ works2df <- function(data, abstract = TRUE, verbose = TRUE) { if (!is.null(paper$primary_location)) { so_info <- paper$primary_location["source"] - so_info <- if (is.na(so_info)) NA else so_info[[1]] - + so_info <- if (length(so_info[[1]]) == 0) NA else so_info[[1]] + venue_info <- replace_w_na(paper$primary_location[venue_cols]) venue <- setNames( - c(paper$primary_location[venue_cols], so_info[so_cols]), + c(venue_info, so_info[so_cols]), c(names(venue_cols), names(so_cols)) ) } @@ -208,12 +212,11 @@ works2df <- function(data, abstract = TRUE, verbose = TRUE) { } first_inst <- prepend(first_inst, "institution") aff_raw <- list(au_affiliation_raw = l$raw_affiliation_string[1]) - l_author <- l_author <- if (length(l$author) > 0) { - prepend(l$author, "au") + l_author <- if (length(l$author) > 0) { + prepend(replace_w_na(l$author), "au") } else { empty_list(c("au_id", "au_display_name", "au_orcid")) } - c(l_author, l["author_position"], aff_raw, first_inst) }), "rbind_df" ) @@ -223,8 +226,9 @@ works2df <- function(data, abstract = TRUE, verbose = TRUE) { if (!is.null(paper$abstract_inverted_index) && abstract) { ab <- abstract_build(paper$abstract_inverted_index) } + paper_biblio <- replace_w_na(paper$biblio) # TODO replace sapply with something else - out_ls <- c(sim_fields, venue, paper$biblio, list(author = author, ab = ab)) + out_ls <- c(sim_fields, venue, paper_biblio, list(author = author, ab = ab)) out_ls[sapply(out_ls, is.null)] <- NULL list_df[[i]] <- out_ls } @@ -337,7 +341,7 @@ authors2df <- function(data, verbose = TRUE) { } sub_affiliation <- prepend(sub_affiliation, "affiliation") } - + sub_affiliation <- replace_w_na(sub_affiliation) list_df[[i]] <- c(sim_fields, sub_affiliation) } @@ -729,6 +733,7 @@ funders2df <- function(data, verbose = TRUE) { } out_df <- rbind_oa_ls(list_df) + out_df } @@ -819,6 +824,7 @@ sources2df <- function(data, verbose = TRUE) { } out_df <- rbind_oa_ls(list_df) + out_df } @@ -901,6 +907,7 @@ publishers2df <- function(data, verbose = TRUE) { } out_df <- rbind_oa_ls(list_df) + out_df } diff --git a/R/utils.R b/R/utils.R index 4b878b7..8bf4878 100644 --- a/R/utils.R +++ b/R/utils.R @@ -10,6 +10,10 @@ simple_rapply <- function(x, fn, ...) { `%||%` <- function(x, y) if (is.null(x)) y else x +replace_w_na <- function(x){ + lapply(x, `%||%`, y = NA) +} + subs_na <- function(x, type = c("row_df", "col_df", "flat", "rbind_df", "identical"), prefix = NULL) { type <- match.arg(type) if (length(x) == 0) { @@ -21,10 +25,11 @@ subs_na <- function(x, type = c("row_df", "col_df", "flat", "rbind_df", "identic } out <- switch(type, - row_df = as.data.frame(x), + row_df = as.data.frame(replace_w_na(x)), col_df = tibble::enframe(unlist(x)), flat = unlist(x), - rbind_df = do.call(rbind.data.frame, x) + rbind_df = do.call(rbind.data.frame, lapply(x, replace_w_na) + ) ) if (!is.null(prefix)) { diff --git a/man/authors2df.Rd b/man/authors2df.Rd index 74fe398..6859228 100644 --- a/man/authors2df.Rd +++ b/man/authors2df.Rd @@ -9,8 +9,9 @@ authors2df(data, verbose = TRUE) \arguments{ \item{data}{List. Output of \code{oa_request}.} -\item{verbose}{Logical. If TRUE, print information on querying process. -Default to \code{verbose = FALSE}.} +\item{verbose}{Logical. +If TRUE, print information about the dataframe conversion process. +Defaults to TRUE.} } \value{ a data.frame. diff --git a/man/concepts2df.Rd b/man/concepts2df.Rd index 22343f2..d9c23a7 100644 --- a/man/concepts2df.Rd +++ b/man/concepts2df.Rd @@ -9,8 +9,9 @@ concepts2df(data, verbose = TRUE) \arguments{ \item{data}{List. Output of \code{oa_request}.} -\item{verbose}{Logical. If TRUE, print information on querying process. -Default to \code{verbose = FALSE}.} +\item{verbose}{Logical. +If TRUE, print information about the dataframe conversion process. +Defaults to TRUE.} } \value{ a data.frame. diff --git a/man/funders2df.Rd b/man/funders2df.Rd index 4c0d785..b6fc8fd 100644 --- a/man/funders2df.Rd +++ b/man/funders2df.Rd @@ -9,8 +9,9 @@ funders2df(data, verbose = TRUE) \arguments{ \item{data}{List. Output of \code{oa_request}.} -\item{verbose}{Logical. If TRUE, print information on querying process. -Default to \code{verbose = FALSE}.} +\item{verbose}{Logical. +If TRUE, print information about the dataframe conversion process. +Defaults to TRUE.} } \value{ a data.frame. diff --git a/man/institutions2df.Rd b/man/institutions2df.Rd index 596774a..f252fe7 100644 --- a/man/institutions2df.Rd +++ b/man/institutions2df.Rd @@ -9,8 +9,9 @@ institutions2df(data, verbose = TRUE) \arguments{ \item{data}{List. Output of \code{oa_request}.} -\item{verbose}{Logical. If TRUE, print information on querying process. -Default to \code{verbose = FALSE}.} +\item{verbose}{Logical. +If TRUE, print information about the dataframe conversion process. +Defaults to TRUE.} } \value{ a data.frame. diff --git a/man/oa2df.Rd b/man/oa2df.Rd index 5233fc4..330509a 100644 --- a/man/oa2df.Rd +++ b/man/oa2df.Rd @@ -30,8 +30,9 @@ See more at 100) }) +test_that("oa_fetch works with 1 identifier", { + w <- oa_fetch(identifier = "W3046863325") # Work + a <- oa_fetch(identifier = "A1969205032") # Author + i <- oa_fetch(identifier = "I4200000001") # Institution + f <- oa_fetch(identifier = "F4320332161") # Funder + p <- oa_fetch(identifier = "P4310311775") # Publisher + s <- oa_fetch(identifier = "S1983995261") # Source + co <- oa_fetch(identifier = "C2522767166") # Concept + + expect_s3_class(w, "data.frame") + expect_s3_class(a, "data.frame") + expect_s3_class(i, "data.frame") + expect_s3_class(f, "data.frame") + expect_s3_class(p, "data.frame") + expect_s3_class(s, "data.frame") + expect_s3_class(co, "data.frame") + + expect_equal(dim(w), c(1, 30)) + expect_equal(dim(a), c(1, 15)) + expect_equal(dim(i), c(1, 21)) + expect_equal(dim(f), c(1, 17)) + expect_equal(dim(p), c(1, 19)) + expect_equal(dim(s), c(1, 26)) + expect_equal(dim(co), c(1, 16)) + +})