-
Notifications
You must be signed in to change notification settings - Fork 21
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
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5beb0a
Add argument to save pages returned by oa_request
rkrug d1f78ed
Correction in man
rkrug fcbd5ef
Add regenerated man page
rkrug 07b9721
Update name and documentatuion changes
rkrug fdbc79f
Merge branch 'main' into save_pages
rkrug 7a891df
Ading function argument to save pages
rkrug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ vignettes/*.R | |
vignettes/*.html | ||
concept-abbre.csv | ||
dev/ | ||
.lintr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
#' 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. | ||
|
@@ -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> | ||
|
@@ -303,6 +314,7 @@ | |
oa_request <- function(query_url, | ||
per_page = 200, | ||
paging = "cursor", | ||
save_pages = NULL, | ||
count_only = FALSE, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
|
@@ -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) | ||
} | ||
save_pages <- normalizePath(save_pages) | ||
rkrug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result <- character(n_pages) | ||
|
||
save_no_pages <- 20 | ||
} | ||
|
||
# Activation of cursor pagination | ||
next_page <- get_next_page(paging, 1) | ||
data <- vector("list", length = n_pages) | ||
|
@@ -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 | ||
} 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) | ||
} | ||
} | ||
|
||
get_next_page <- function(paging, i, res = NULL) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
: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?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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out_dir
sounds a bit like where to save the output - which it is not. Ratheroutput_pages_to
?There was a problem hiding this comment.
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!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in last commit