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

Save pages returned by oa_request() and do not process further #181

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ vignettes/*.R
vignettes/*.html
concept-abbre.csv
dev/
.lintr
39 changes: 36 additions & 3 deletions R/oa_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@
#' Either "cursor" for cursor paging or "page" for basic paging.
#' When used with options$sample, please set `paging = "page"`
#' to avoid duplicates.
#' @param save_pages Character.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Two quick thoughts about save_pages:

  1. I think the name could be a little clearer - people might confuse this with a toggle option taking T/F. Maybe something like out_dir that makes it explicit that it expects a directory path as value?

  2. The argument description should ideally be concise and document just its function. The motivation and other notes (about memory issues, etc.) could move to its own @section or @notes

Copy link
Author

Choose a reason for hiding this comment

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

  1. Agreed. out_dir sounds a bit like where to save the output - which it is not. Rather output_pages_to?
  2. OK - I will streamline it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah output_pages_to sounds great!

Copy link
Author

Choose a reason for hiding this comment

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

Changed in last commit

#' If NULL, the individual pages will be downloaded and processed in memory.
#' This can, lead to memory issues if the amount downloaded is to large. To
#' avoid this, set `save_pages` to a directory path. The individual pages
#' downloaded will be saved in the directory specified by `save_pages`.
#' The directory will be created if it dowes not exist.
#' **The function will overwrite existing files in the directory without
#' warning!**
#' Defaults to NULL.
#' @param count_only Logical.
#' If TRUE, the function returns only the number of item matching the query.
#' Defaults to FALSE.
Expand All @@ -173,7 +182,9 @@
#' @param verbose Logical.
#' If TRUE, print information about the querying process. Defaults to TRUE.
#'
#' @return a data.frame or a list of bibliographic records.
#' @return a data.frame or a list of bibliographic records. If `save_pages` is
#' not NULL a character vector containing the names of the saved pages
#' is returned.
#'
#' For more extensive information about OpenAlex API, please visit:
#' <https://docs.openalex.org>
Expand Down Expand Up @@ -303,6 +314,7 @@
oa_request <- function(query_url,
per_page = 200,
paging = "cursor",
save_pages = NULL,
count_only = FALSE,
Copy link
Author

Choose a reason for hiding this comment

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

This is not more than a proof of concept implementation.

mailto = oa_email(),
api_key = oa_apikey(),
Expand Down Expand Up @@ -361,6 +373,17 @@
# Setting items per page
query_ls[["per-page"]] <- per_page

# Setup save_pages if not NULL
if (!is.null(save_pages)) {
if (!dir.exists(save_pages)) {
dir.create(save_pages)

Check warning on line 379 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L378-L379

Added lines #L378 - L379 were not covered by tests
}
save_pages <- normalizePath(save_pages)
rkrug marked this conversation as resolved.
Show resolved Hide resolved
result <- character(n_pages)

Check warning on line 382 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L381-L382

Added lines #L381 - L382 were not covered by tests

save_no_pages <- 20

Check warning on line 384 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L384

Added line #L384 was not covered by tests
}

# Activation of cursor pagination
next_page <- get_next_page(paging, 1)
data <- vector("list", length = n_pages)
Expand All @@ -370,10 +393,20 @@
query_ls[[paging]] <- next_page
res <- api_request(query_url, ua, query = query_ls)
next_page <- get_next_page(paging, i + 1, res)
if (!is.null(res$results)) data[[i]] <- res$results
if (!is.null(save_pages)) {
fn <- file.path(save_pages, paste0("page_", i, ".rds"))
saveRDS(res, file.path(save_pages, paste0("page_", i, ".rds")))
result[[i]] <- fn

Check warning on line 399 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L397-L399

Added lines #L397 - L399 were not covered by tests
} else {
if (!is.null(res$results)) data[[i]] <- res$results
}
}

unlist(data, recursive = FALSE)
if (is.null(save_pages)) {
return(unlist(data, recursive = FALSE))
} else {
return(result)

Check warning on line 408 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L408

Added line #L408 was not covered by tests
}
}

get_next_page <- function(paging, i, res = NULL) {
Expand Down
Loading