Skip to content

Commit 558581c

Browse files
authored
Merge pull request #214 from yjunechoe/paper-topics
add Work `topics` field
2 parents 3166395 + 7e42d9f commit 558581c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

R/oa2df.R

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ works2df <- function(data, abstract = TRUE, verbose = TRUE,
143143
"is_oa_anywhere", "oa_status", "oa_url", "any_repository_has_fulltext",
144144
"language", "grants", "cited_by_count", "counts_by_year",
145145
"publication_year", "cited_by_api_url", "ids", "doi", "type",
146-
"referenced_works", "related_works", "is_paratext", "is_retracted", "concepts"
146+
"referenced_works", "related_works", "is_paratext", "is_retracted",
147+
"concepts", "topics"
147148
)
148149
works_process <- tibble::tribble(
149150
~type, ~field,
@@ -245,7 +246,31 @@ works2df <- function(data, abstract = TRUE, verbose = TRUE,
245246
names(open_access)[[1]] <- "is_oa_anywhere"
246247
}
247248

248-
out_ls <- c(sim_fields, venue, open_access, paper_biblio, list(author = author, ab = ab))
249+
# Topics
250+
process_paper_topics <- function(paper) {
251+
topics <- paper$topics
252+
if (is.null(topics)) return(NULL)
253+
topics_ls <- lapply(seq_along(topics), function(i) {
254+
topic <- topics[[i]]
255+
relev <- c(
256+
# Hoist fields for the topic entity
257+
list(topic = topic[c("id", "display_name")]),
258+
# Keep info about other entities as-is
259+
topic[vapply(topic, is.list, logical(1))]
260+
)
261+
relev_df <- subs_na(relev, "rbind_df")[[1]]
262+
relev_df <- tibble::rownames_to_column(relev_df, "name")
263+
cbind(i = i, score = topic$score, relev_df)
264+
})
265+
topics_df <- do.call(rbind.data.frame, topics_ls)
266+
list(tibble::as_tibble(topics_df))
267+
}
268+
topics <- process_paper_topics(paper)
269+
270+
out_ls <- c(
271+
sim_fields, venue, open_access, paper_biblio,
272+
list(author = author, ab = ab, topics = topics)
273+
)
249274
out_ls[sapply(out_ls, is.null)] <- NULL
250275
list_df[[i]] <- out_ls
251276
}

tests/testthat/test-oa_fetch.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ test_that("oa_fetch works with 1 identifier", {
364364
expect_s3_class(s, "data.frame")
365365
expect_s3_class(co, "data.frame")
366366

367-
expect_equal(dim(w), c(1, 36))
367+
expect_equal(dim(w), c(1, 37))
368368
expect_equal(dim(a), c(1, 17))
369369
expect_equal(dim(i), c(1, 21))
370370
expect_equal(dim(f), c(1, 17))

0 commit comments

Comments
 (0)