Skip to content

Commit

Permalink
Cache requests (#132)
Browse files Browse the repository at this point in the history
* add caching of requests

* add news item

* add note about caching/downloading

* Update documentation

* add memoise to imports

---------

Co-authored-by: sbfnk <sbfnk@users.noreply.github.com>
  • Loading branch information
sbfnk and sbfnk authored Sep 24, 2024
1 parent 3400e32 commit e575c5b
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 18 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
httr,
jsonlite,
lubridate,
memoise,
oai,
wpp2017,
xml2,
Expand All @@ -46,7 +47,7 @@ VignetteBuilder:
Encoding: UTF-8
LazyData: true
NeedsCompilation: no
RoxygenNote: 7.3.0
RoxygenNote: 7.3.2
URL: https://github.com/epiforecasts/socialmixr,
https://epiforecasts.io/socialmixr/
BugReports: https://github.com/epiforecasts/socialmixr/issues
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ importFrom(lifecycle,deprecate_warn)
importFrom(lubridate,period)
importFrom(lubridate,period_to_seconds)
importFrom(lubridate,years)
importFrom(memoise,memoise)
importFrom(oai,list_records)
importFrom(stats,median)
importFrom(stats,runif)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* The speed of loading surveys has been increased.
* An error has been fixed causing NA contact matrices if any 5-year age band in the population data was missing.
* Results of function calls accessing Zenodo repository are now cached for speedup and to avoid multiple web requests

# socialmixr 0.3.2

Expand Down
26 changes: 21 additions & 5 deletions R/get_survey.r
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
#' Get a survey, either from its Zenodo repository, a set of files, or a survey variable
#
#'
#' @description Downloads survey data, or extracts them from files, and returns a clean data set.
#' @param survey a DOI or url to get the survey from, or a [survey()] object (in which case only cleaning is done).
#' @param ... options for [clean()], which is called at the end of this
#' @autoglobal
#' Downloads survey data, or extracts them from files, and returns a clean data set. If a survey URL is accessed multiple times, the data will be cached (unless `clear_cache` is set to `TRUE`) to avoid repeated downloads.
#'
#' If survey objects are used repeatedly the downloaded files can be saved and reloaded between sessions then survey objects can be saved/loaded using [base::saveRDS()] and [base::readRDS()], or via the individual survey files that can be downloaded using [download_survey()] and subsequently loaded using [load_survey()].
#' @param clear_cache logical, whether to clear the cache before downloading the survey; by default, the cache is not cleared and so multiple calls of this function to access the same survey will not result in repeated downloads
#' @importFrom memoise memoise
#' @inheritParams .get_survey
#' @examples
#' \dontrun{
#' list_surveys()
#' peru_survey <- get_survey("https://doi.org/10.5281/zenodo.1095664")
#' }
#' @return a survey in the correct format
#' @export
get_survey <- function(survey, ...) {
get_survey <- function(survey, clear_cache = FALSE, ...) {
if (!("get_survey" %in% names(.socialmixr.env$cached_functions)) ||
clear_cache) {
.socialmixr.env$cached_functions$get_survey <- memoise(.get_survey)
}
.socialmixr.env$cached_functions$get_survey(survey, ...)
}

#' Internal function to get survey data
#' @autoglobal
#' @param survey a DOI or url to get the survey from, or a [survey()] object (in which case only cleaning is done).
#' @param ... options for [clean()], which is called at the end of this
#' @keywords internal
.get_survey <- function(survey, ...) {
if (inherits(survey, "survey")) {
new_survey <- survey
} else {
Expand Down
12 changes: 6 additions & 6 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ utils::globalVariables(c(
"proportion", # <contact_matrix>
"participants", # <contact_matrix>
"file_name", # <download_survey>
"common_doi", # <list_surveys>
"doi.nb", # <list_surveys>
"identifier.1", # <list_surveys>
"title", # <list_surveys>
"creator", # <list_surveys>
"identifier.2", # <list_surveys>
"common_doi", # <.list_surveys>
"doi.nb", # <.list_surveys>
"identifier.1", # <.list_surveys>
"title", # <.list_surveys>
"creator", # <.list_surveys>
"identifier.2", # <.list_surveys>
"..main_id", # <load_survey>
"..merge_id", # <load_survey>
"..original.lower.age.limit", # <pop_age>
Expand Down
16 changes: 13 additions & 3 deletions R/lists.r
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
#' List all surveys available for download
#'
#' @return character vector of surveys
#' @importFrom oai list_records
#' @autoglobal
#' @inheritParams get_survey
#' @examples
#' \dontrun{
#' list_surveys()
#' }
#' @export
list_surveys <- function() {
list_surveys <- function(clear_cache = FALSE) {
if (!("list_surveys" %in% names(.socialmixr.env$cached_functions)) ||
clear_cache) {
.socialmixr.env$cached_functions$list_surveys <- memoise(.list_surveys)
}
.socialmixr.env$cached_functions$list_surveys()
}

#' @autoglobal
#' @importFrom oai list_records
#' @keywords internal
.list_surveys <- function() {
record_list <-
data.table(list_records("https://zenodo.org/oai2d",
metadataPrefix = "oai_datacite",
Expand Down
2 changes: 2 additions & 0 deletions R/socialmixr-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.socialmixr.env <- new.env(parent = emptyenv())
assign("cached_functions", list(), envir = .socialmixr.env)
17 changes: 17 additions & 0 deletions man/dot-get_survey.Rd

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

9 changes: 7 additions & 2 deletions man/get_survey.Rd

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

5 changes: 4 additions & 1 deletion man/list_surveys.Rd

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

0 comments on commit e575c5b

Please sign in to comment.