|
28 | 28 | #' @param journalSource (character) Journal of publication.
|
29 | 29 | #' @param journalPublisher (character) Publisher of journal.
|
30 | 30 | #' @param flatten (logical) should any lists in the resulting data be flattened
|
31 |
| -#' into comma-seperated strings? |
| 31 | +#' into comma-seperated strings? Ignored in lit_export. |
| 32 | +#' @param abstract (logical) should the abstract be included in the results. |
| 33 | +#' Ignored for lit_search. |
32 | 34 | #' @param limit how many records to return. limit=NULL will fetch up to 10,000.
|
33 | 35 | #' @param curlopts list of named curl options passed on to HttpClient.
|
34 | 36 | #' see curl::curl_options for curl options.
|
@@ -127,6 +129,7 @@ lit_search <- function(
|
127 | 129 | journalSource=NULL,
|
128 | 130 | journalPublisher=NULL,
|
129 | 131 | flatten=TRUE,
|
| 132 | + abstract=FALSE, |
130 | 133 | limit=NULL,
|
131 | 134 | curlopts = list()
|
132 | 135 | ) {
|
@@ -226,7 +229,7 @@ lit_search <- function(
|
226 | 229 | urls <- sapply(urls,function(x) gsub("\\[|\\]","",x)) # remove any square brackets
|
227 | 230 | # make request
|
228 | 231 | ll <- gbif_async_get(urls,parse=TRUE)
|
229 |
| - data <- process_lit_async_results(ll,flatten=flatten) |
| 232 | + data <- process_lit_async_results(ll,flatten=flatten,abstract=abstract) |
230 | 233 | meta <- rgbif_compact(ll[[length(urls)]])
|
231 | 234 | # clean results
|
232 | 235 | meta$results <- NULL
|
@@ -282,12 +285,95 @@ lit_count <- function(...) {
|
282 | 285 | count
|
283 | 286 | }
|
284 | 287 |
|
285 |
| -process_lit_async_results <- function(ll,flatten=TRUE) { |
| 288 | + |
| 289 | +#' @export |
| 290 | +#' @rdname lit_search |
| 291 | +lit_export <- function( |
| 292 | + q=NULL, |
| 293 | + countriesOfResearcher=NULL, |
| 294 | + countriesOfCoverage=NULL, |
| 295 | + literatureType=NULL, |
| 296 | + relevance=NULL, |
| 297 | + year=NULL, |
| 298 | + topics=NULL, |
| 299 | + datasetKey=NULL, |
| 300 | + publishingOrg=NULL, |
| 301 | + peerReview=NULL, |
| 302 | + openAccess=NULL, |
| 303 | + downloadKey=NULL, |
| 304 | + doi=NULL, |
| 305 | + journalSource=NULL, |
| 306 | + journalPublisher=NULL, |
| 307 | + flatten=NULL, |
| 308 | + abstract=FALSE, |
| 309 | + limit=NULL, |
| 310 | + curlopts = NULL |
| 311 | + ) { |
| 312 | + |
| 313 | + if(!is_uuid(datasetKey) & !is.null(datasetKey)) stop("'datasetKey' should be a GBIF dataset uuid.") |
| 314 | + if(!is_uuid(publishingOrg) & !is.null(publishingOrg)) stop("'publishingOrg' should be a GBIF publisher uuid.") |
| 315 | + if(!is_download_key(downloadKey) & !is.null(downloadKey)) stop("'downloadKey' should be a GBIF downloadkey.") |
| 316 | + |
| 317 | + assert(q,"character") |
| 318 | + assert(countriesOfResearcher,"character") |
| 319 | + assert(countriesOfCoverage,"character") |
| 320 | + assert(literatureType,"character") |
| 321 | + assert(relevance,"character") |
| 322 | + assert(topics,"character") |
| 323 | + assert(peerReview,"logical") |
| 324 | + assert(openAccess,"logical") |
| 325 | + assert(doi,"character") |
| 326 | + assert(journalSource,"character") |
| 327 | + assert(journalPublisher,"character") |
| 328 | + if(!is.null(flatten)) message("flatten argument is ignored for lit_export, results are returned flat.") |
| 329 | + if(!is.null(limit)) message("limit argument is ignored for lit_export, all results are returned.") |
| 330 | + if(!is.null(curlopts)) message("curlopts argument are ignored for lit_export") |
| 331 | + # https://api.gbif.org/v1/literature/export?format=TSV&gbifDownloadKey=0138953-210914110416597 |
| 332 | + |
| 333 | + args <- rgbif_compact( |
| 334 | + list(q = q, |
| 335 | + year = year, |
| 336 | + peerReview = peerReview, |
| 337 | + openAccess = openAccess |
| 338 | + )) |
| 339 | + |
| 340 | + args <- rgbif_compact( |
| 341 | + c(args, |
| 342 | + convmany(relevance), |
| 343 | + convmany(countriesOfResearcher), |
| 344 | + convmany(countriesOfCoverage), |
| 345 | + convmany(literatureType), |
| 346 | + convmany(topics), |
| 347 | + convmany_rename(datasetKey,"gbifDatasetKey"), |
| 348 | + convmany_rename(publishingOrg,"publishingOrganizationKey"), |
| 349 | + convmany_rename(downloadKey,"gbifDownloadKey"), |
| 350 | + convmany(doi), |
| 351 | + convmany_rename(journalSource,"source"), |
| 352 | + convmany_rename(journalPublisher,"publisher") |
| 353 | + )) |
| 354 | + |
| 355 | + url_query <- paste0(names(args),"=",args,collapse="&") |
| 356 | + url_query <- utils::URLencode(url_query) |
| 357 | + url <- paste0(gbif_base(),"/literature/export?",url_query) |
| 358 | + temp_file <- tempfile() |
| 359 | + utils::download.file(url,destfile=temp_file,quiet=TRUE) |
| 360 | + out <- tibble::as_tibble(data.table::fread(temp_file, showProgress=FALSE)) |
| 361 | + if(!abstract) out$abstract <- NULL |
| 362 | + colnames(out) <- to_camel(colnames(out)) |
| 363 | + out[] <- lapply(out, as.character) |
| 364 | + out$peerReview <- as.logical(out$peerReview) |
| 365 | + out$openAccess <- as.logical(out$openAccess) |
| 366 | + out[out == ""] <- NA |
| 367 | + out |
| 368 | +} |
| 369 | + |
| 370 | + |
| 371 | +process_lit_async_results <- function(ll,flatten=TRUE,abstract=FALSE) { |
286 | 372 | data_list <- lapply(ll,function(x) x$results)
|
287 | 373 | # handle complex identifiers
|
288 | 374 | data_list <- lapply(data_list,function(x) tibble::tibble(x,x$identifiers))
|
289 | 375 | for(i in 1:length(data_list)) data_list[[i]]$identifiers <- NULL
|
290 |
| - for(i in 1:length(data_list)) data_list[[i]]$abstract <- NULL |
| 376 | + if(!abstract) for(i in 1:length(data_list)) data_list[[i]]$abstract <- NULL |
291 | 377 | data <- bind_rows(data_list)
|
292 | 378 | # data
|
293 | 379 | if(flatten) {
|
|
0 commit comments