Skip to content

Commit

Permalink
fix: check if field is returned by WOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Nov 10, 2023
1 parent 0d3dad4 commit 0311f26
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 39 deletions.
9 changes: 9 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ list_to_df <- function(x) {
x
}
}

get_field <- function(data, field) {

if (!(field %in% colnames(data))) {
return(rep(NA, nrow(data)))
} else {
data[ , field]
}
}
108 changes: 69 additions & 39 deletions R/wos_get_records.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ wos_get_records <- function(query, database = "WOS", limit = NULL, sleep = 1) {
}
}

pages <- seq(1, n_refs, by = n_records_per_page)
pages <- seq(1, ceiling(n_refs / n_records_per_page), by = 1)


for (page in pages) {
Expand Down Expand Up @@ -147,44 +147,74 @@ wos_get_records <- function(query, database = "WOS", limit = NULL, sleep = 1) {

## Convert listed df to df ----

data <- data.frame(
"uid" = content$"uid",
"document_type" = list_to_df(content$"sourceTypes"),
"title" = list_to_df(content$"title"),
"authors" = unlist(lapply(content$"names"$"authors",
function(x) {
if (is.null(x)) {
NA
} else {
paste0(x[ , 1], collapse = " | ")
}})),
"published_year" = list_to_df(content$"source"$"publishYear"),
"published_month" = list_to_df(content$"source"$"publishMonth"),
"source" = list_to_df(content$"source"$"sourceTitle"),
"volume" = list_to_df(content$"source"$"volume"),
"issue" = list_to_df(content$"source"$"issue"),
"pages" = list_to_df(content$"source"$"pages"$"range"),
"no_article" = list_to_df(content$"source"$"articleNumber"),
"supplement_number" = list_to_df(content$"source"$"supplement"),
"special_issue" = list_to_df(content$"source"$"specialIssue"),
"book_editors" = unlist(lapply(content$"names"$"bookEditors",
function(x) {
if (is.null(x)) {
NA
} else {
paste0(x[ , 1], collapse = " | ")
}})),
"keywords" = list_to_df(content$"keywords"$"authorKeywords"),
"doi" = content$"identifiers"$"doi",
"eissn" = content$"identifiers"$"eissn",
"issn" = content$"identifiers"$"issn",
"isbn" = content$"identifiers"$"isbn",
"pmid" = content$"identifiers"$"pmid",
"citations" = unlist(lapply(content$"citations",
function(x) {
ifelse(is.null(x$"count"), NA,
x$"count")
})))
uid <- list_to_df(get_field(content,
"uid"))
document_type <- list_to_df(get_field(content,
"sourceTypes"))
title <- list_to_df(get_field(content,
"title"))

authors <- unlist(lapply(get_field(content$"names",
"authors"),
function(x) {
if (!is.data.frame(x)) {
NA
} else {
paste0(x[ , 1], collapse = " | ")
}}))

published_year <- list_to_df(get_field(content$"source",
"publishYear"))
published_month <- list_to_df(get_field(content$"source",
"publishMonth"))
source <- list_to_df(get_field(content$"source",
"sourceTitle"))
volume <- list_to_df(get_field(content$"source",
"volume"))
issue <- list_to_df(get_field(content$"source",
"issue"))
pages <- list_to_df(get_field(content$"source"$"pages",
"range"))
no_article <- list_to_df(get_field(content$"source",
"articleNumber"))
supplement_number <- list_to_df(get_field(content$"source",
"supplement"))
special_issue <- list_to_df(get_field(content$"source",
"specialIssue"))

book_editors <- unlist(lapply(get_field(content$"names",
"bookEditors"),
function(x) {
if (!is.data.frame(x)) {
NA
} else {
paste0(x[ , 1], collapse = " | ")
}}))

keywords <- list_to_df(get_field(content$"keywords",
"authorKeywords"))
doi <- list_to_df(get_field(content$"identifiers",
"doi"))
eissn <- list_to_df(get_field(content$"identifiers",
"eissn"))
issn <- list_to_df(get_field(content$"identifiers",
"issn"))
isbn <- list_to_df(get_field(content$"identifiers",
"isbn"))
pmid <- list_to_df(get_field(content$"identifiers",
"pmid"))

citations <- unlist(lapply(get_field(content, "citations"),
function(x) {
ifelse(is.na(x$"count"), NA,
x$"count")
}))

data <- data.frame(uid, document_type, title, authors, published_year,
published_month, source, volume, issue, pages,
no_article, supplement_number, special_issue,
book_editors, keywords, doi, eissn, issn, isbn, pmid,
citations)

refs <- rbind(refs, data)

Expand Down

0 comments on commit 0311f26

Please sign in to comment.