From d3dd47ddf5fbda3841ed611245d98cb1da680821 Mon Sep 17 00:00:00 2001 From: Michael Sumner Date: Wed, 31 Jul 2019 02:02:58 +1000 Subject: [PATCH] doc for avoid build-time dependency #76 (#91) * doc for avoid build-time dependency #76 * document() for Rd * use preformatted * No code with preformatted block --- R/memoise.R | 16 ++++++++++++++++ man/memoise.Rd | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/R/memoise.R b/R/memoise.R index 48915d5..1f24fe0 100644 --- a/R/memoise.R +++ b/R/memoise.R @@ -32,6 +32,22 @@ #' repeated. #' } #' +#' It is recommended that functions in a package are not memoised at build-time, +#' but when the package is loaded. The simplest way to do this is within +#' \code{.onLoad()} with, for example +#' +#' +#' \preformatted{ +#' # file.R +#' fun <- function() { +#' some_expensive_process() +#' } +#' +#' # zzz.R +#' .onLoad <- function(pkgname, libname) { +#' fun <<- memoise::memoise(fun) +#' } +#' } #' @name memoise #' @title Memoise a function. #' @param f Function of which to create a memoised copy. diff --git a/man/memoise.Rd b/man/memoise.Rd index c4fdfe1..fbdce05 100644 --- a/man/memoise.Rd +++ b/man/memoise.Rd @@ -52,6 +52,22 @@ Two example situations where \code{memoise} could be of use: once at the R prompt, or put it somewhere else where it won't get repeated. } + +It is recommended that functions in a package are not memoised at build-time, +but when the package is loaded. The simplest way to do this is within +\code{.onLoad()} with, for example + +\preformatted{ +# file.R +fun <- function() { + some_expensive_process() +} + +# zzz.R +.onLoad <- function(pkgname, libname) { + fun <<- memoise::memoise(fun) +} +} } \examples{ # a() is evaluated anew each time. memA() is only re-evaluated