Skip to content

Commit

Permalink
incorporate June's comments and ready to remove simple_rapply
Browse files Browse the repository at this point in the history
  • Loading branch information
trangdata committed Jul 19, 2023
1 parent b71375f commit 0a1979e
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 28 deletions.
23 changes: 15 additions & 8 deletions R/oa2df.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
)
}
Expand All @@ -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"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -729,6 +733,7 @@ funders2df <- function(data, verbose = TRUE) {
}

out_df <- rbind_oa_ls(list_df)
out_df
}


Expand Down Expand Up @@ -819,6 +824,7 @@ sources2df <- function(data, verbose = TRUE) {
}

out_df <- rbind_oa_ls(list_df)
out_df
}


Expand Down Expand Up @@ -901,6 +907,7 @@ publishers2df <- function(data, verbose = TRUE) {
}

out_df <- rbind_oa_ls(list_df)
out_df
}


Expand Down
9 changes: 7 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions man/authors2df.Rd

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

5 changes: 3 additions & 2 deletions man/concepts2df.Rd

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

5 changes: 3 additions & 2 deletions man/funders2df.Rd

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

5 changes: 3 additions & 2 deletions man/institutions2df.Rd

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

5 changes: 3 additions & 2 deletions man/oa2df.Rd

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

5 changes: 3 additions & 2 deletions man/publishers2df.Rd

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

5 changes: 3 additions & 2 deletions man/sources2df.Rd

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

5 changes: 3 additions & 2 deletions man/venues2df.Rd

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

5 changes: 3 additions & 2 deletions man/works2df.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-oa_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,29 @@ test_that("oa_fetch works for publishers", {
expect_true(nrow(s) > 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))

})

0 comments on commit 0a1979e

Please sign in to comment.