Skip to content

Allow multiple DOIs for cr_abstract - or fail more gracefully #252

@LukasWallrich

Description

@LukasWallrich

Could cr_abstract accept multiple DOI - like most functions in the package?

If not, a clearer error message would help:

library(rcrossref)
cr_abstract(cr_r(2))
#> Error in exists(z, private$crul_h_pool): first argument has length > 1

With some digging, I realised that I can already use cr_works for that, which is much faster than looping over cr_abstract. Is there an advantage to the current cr_abstract implementation? Or could it just be a wrapper for cr_works?

library(rcrossref)

#Using cr_abstract
many_abstracts1 <- function(dois) {
  lapply(dois, function(doi) {
    res <- try(cr_abstract(doi), silent = TRUE)
    if (inherits(res, "try-error")) {
      return(NA_character_)
    } else {
      return(res)
    }
  })
}

#Using cr_works
many_abstracts2 <- function(dois) {
  filt <- stats::setNames(dois, rep("doi", length(dois)))
  res  <- rcrossref::cr_works(
    filter = filt,
    select = c("DOI", "abstract"),
    limit  = length(dois)
  )
  
  res$data$abstract
  
}

my_dois <- cr_r(100)

system.time(
  abs1 <- many_abstracts1(my_dois)
)
#>    user  system elapsed 
#>   0.411   0.076  13.175

sum(is.na(abs1))
#> [1] 75

system.time(
  abs2 <- many_abstracts2(my_dois)
)
#>    user  system elapsed 
#>   0.050   0.000   0.191

sum(is.na(abs2))
#> [1] 74

Created on 2025-10-02 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions