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 5 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
42 changes: 39 additions & 3 deletions R/oa_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@
#' See https://docs.openalex.org/how-to-use-the-api/get-lists-of-entities/paging.
#' @param pages Integer vector.
#' The range of pages to return. If NULL, return all pages.
#' @param output_pages_to Character.
#' If NULL, the individual pages will be downloaded and processed in memory.
#' If not NULL, the individual pages
#' downloaded will be saved in the directory specified by `output_pages_to`.
#' The directory will be created if it does 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 @@ -181,7 +189,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 `output_pages_to` 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 @@ -312,6 +322,7 @@
per_page = 200,
paging = "cursor",
pages = NULL,
output_pages_to = 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 @@ -379,6 +390,17 @@
# Setting items per page
query_ls[["per-page"]] <- per_page

# Setup output_pages_to if not NULL
if (!is.null(output_pages_to)) {
output_pages_to <- normalizePath(output_pages_to, mustWork = FALSE)
if (!dir.exists(output_pages_to)) {
dir.create(output_pages_to)

Check warning on line 397 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L395-L397

Added lines #L395 - L397 were not covered by tests
}
result <- character(n_pages)

Check warning on line 399 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L399

Added line #L399 was not covered by tests

save_no_pages <- 20

Check warning on line 401 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L401

Added line #L401 was not covered by tests
}

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

Check warning on line 420 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L415-L420

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

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

Check warning on line 429 in R/oa_fetch.R

View check run for this annotation

Codecov / codecov/patch

R/oa_fetch.R#L429

Added line #L429 was not covered by tests
}
}

get_next_page <- function(paging, i, res = NULL) {
Expand Down
14 changes: 13 additions & 1 deletion man/oa_request.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading