Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix apc processing for W2101805856 #306

Merged
merged 4 commits into from
Dec 29, 2024
Merged

fix apc processing for W2101805856 #306

merged 4 commits into from
Dec 29, 2024

Conversation

trangdata
Copy link
Collaborator

Closes #305.

library(openalexR)
paper <- oa_fetch(identifier = "W2101805856", output = "list")
paper$apc_list
#> $value
#> [1] 3500000
#> 
#> $currency
#> [1] "IRR"
#> 
#> $value_usd
#> NULL
#> 
#> $provenance
#> [1] "doaj"
if (any(lengths(paper[c("apc_list", "apc_paid")]) > 0)) {
  apc_fields <- list(value = NA, currency = NA, value_usd = NA, provenance = NA)
  apc <- list(rbind.data.frame(list(
    c(type = "list", utils::modifyList(apc_fields, as.list(paper$apc_list))),
    c(type = "paid", utils::modifyList(apc_fields, as.list(paper$apc_paid)))
  )))
}
#> Error in rbind.data.frame(list(c(type = "list", utils::modifyList(apc_fields, : invalid list argument: all variables should have the same length

# my current fix is to use `replace_w_na` but open to more elegant solution
if (any(lengths(paper[c("apc_list", "apc_paid")]) > 0)) {
  apc_fields <- list(value = NA, currency = NA, value_usd = NA, provenance = NA)
  apc <- list(rbind.data.frame(
    c(type = "list", utils::modifyList(apc_fields, openalexR:::replace_w_na(paper$apc_list))),
    c(type = "paid", utils::modifyList(apc_fields, openalexR:::replace_w_na(paper$apc_paid)))
  ))
}
apc
#> [[1]]
#>   type   value currency value_usd provenance
#> 1 list 3500000      IRR        NA       doaj
#> 2 paid      NA     <NA>        NA       <NA>

Created on 2024-12-20 with reprex v2.1.1

@yjunechoe
Copy link
Collaborator

Thanks! Agreed that NULL needs special handling here.

I pushed a minor edit: instead of resolving them to the logical NA via replace_w_na(), I'm attempting a (more verbose) option of removing NULL entries and coalescing them with appropriately-typed NAs (i.e., making apc_fields more robust).

LMK how this looks!

tibble::as_tibble(oa_fetch(identifier = "W2101805856")$apc[[1]])
#> # A tibble: 2 × 5
#>   type    value currency value_usd provenance
#>   <chr>   <dbl> <chr>        <dbl> <chr>     
#> 1 list  3500000 IRR             NA doaj      
#> 2 paid       NA <NA>            NA <NA>

@trangdata
Copy link
Collaborator Author

Looks great @yjunechoe. I considered this as well (and also something like apc_list[sapply(apc_list, is.null)] <- NULL) and ended up just picking one but I'm glad you have a preference. I can merge when you approve!

Copy link
Collaborator

@yjunechoe yjunechoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge!

@trangdata trangdata merged commit e1aa4cd into main Dec 29, 2024
7 checks passed
@trangdata trangdata deleted the fix-apc branch December 29, 2024 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Number of columns unmatched for rbind in works2df function (development version)
2 participants