-
Notifications
You must be signed in to change notification settings - Fork 57
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
Document how to avoid build-time dependency #76
Comments
Oh magic, thanks! I was looking for this advice in
That would have worked for me in readme and/or help. |
Added a PR for this |
It's a kind of magic indeed. Could I also update my memoised function at run-time like this? E.g. I have defined my function The following throws an error, because config_fn <- function(cache = memoise::cache_memory()) {
fn <<- memoise::memoise(fn), cache = cache))
} I guess one can use config_fn <- function(cache = memoise::cache_memory()) {
fn <<- memoise::memoise(environment(fn)[["_f"]], cache = cache)
} Still this does not seem to work - the cache does not get invalidated - so probably my understanding of (recursively updating) environments is flawed? |
FWIW, @richfitz mentioned elsewhere that he has had a CRAN rejection 'for using |
@dpprdan No, that's just silly, and he/we need to push back on this. We clearly do not modify the global environment. We modify the package's namespace. |
Appreciated this thread a bunch! I too nearly ran into a CRAN rejection for "global environment" but was able to demonstrate (with the help of this thread and Gabor's previous one) that it does not modify the global environment. I supplied a screenshot to help support it, leaving it here for future me and others: |
Bummer, I've just run into this as well :/ |
@tyluRp Is this an initial submission to CRAN or a package update? |
@tyluRp this is fightable - I did end up winning on this! See the screenshot I had, where librarying the package + printing the global environment with all names via Also explain that you're modifying the package NS on load and nothing else? |
* In order to prevent build time dependency on 'memoise' we define `img_read_memoised()` at build time and then replace it at load time with a memoised version within `.onLoad()` * This is recommended in <http://memoise.r-lib.org/reference/memoise.html#details> * See <r-lib/memoise#76> for further details * On some platforms doing this also prevents an R CMD check NOTE.
Coming back to this after discovering cache_function <- function(function_name, duration = 86400, omit_args = c()) {
fn <- get(function_name, envir = rlang::ns_env("ffscrapr"))
fn <- memoise::memoise(
fn,
~ memoise::timeout(duration),
omit_args = c(),
cache = cachem::cache_memory()
)
assign(function_name, fn, envir = rlang::ns_env("ffscrapr"))
return(invisible(TRUE))
}
.onLoad <- function(libname, pkgname) {
lapply(c("ff_rosters", "mfl_players"), cache_function)
}
|
This is great, I tweaked it a little bit to allow for assigning separate "prefixed" memoised functions if that fits your use case: # internal ----------------------------------------------------------------
#' @keywords internal
#' @noRd
#' @importFrom memoise memoise timeout
#' @importFrom cachem cache_memory
#' @importFrom rlang ns_env
#' @importFrom glue glue
#' @importFrom cli cli_alert_info
.cache_function <- function(
function_name,
pkg,
duration = 86400,
omit_args = c(),
cache = cachem::cache_mem(),
rename_prefix = "mem_",
quiet = TRUE,
...
) {
fn <- base::get(function_name, envir = rlang::ns_env(pkg))
mem_fn <- memoise::memoise(
fn,
~ memoise::timeout(duration),
omit_args = omit_args,
cache = cache
)
mem_function_name <- glue::glue("{rename_prefix}{function_name}")
assign(mem_function_name, mem_fn, envir = rlang::ns_env(pkg))
if (!quiet) {
cli::cli_alert_info("Created a cached function for {.field {function_name}} as {.field {mem_function_name}}.")
cli::cli_alert_info("The cache will expire in {.field {duration}} seconds.")
}
return(invisible(TRUE))
}
# onLoad ------------------------------------------------------------------
#' @keywords internal
#' @noRd
#' @importFrom purrr walk
.onLoad <- function(libname, pkgname) {
# cache functions ---------------------------------------------------------
c(
"get_entrata_reports_list",
"get_entrata_report_info",
"get_latest_report_version",
"get_property_ids_filter_param"
) |>
purrr::walk(.cache_function, pkg = pkgname, quiet = FALSE)
} |
Following @gaborcsardi's advice in https://stat.ethz.ch/pipermail/r-devel/2017-January/073647.html, I tried memoising in
.onLoad()
, and it worked for me:This eliminates the build-time dependency on memoise and also gets rid of
R CMD check
warnings about using memoise without importing it.I wonder if and where we should document this.
The text was updated successfully, but these errors were encountered: