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

How to memoise a function in a package #105

Closed
MalteThodberg opened this issue Jan 21, 2020 · 2 comments
Closed

How to memoise a function in a package #105

MalteThodberg opened this issue Jan 21, 2020 · 2 comments

Comments

@MalteThodberg
Copy link

I'm making a package with RStudio+roxygen2, and I would like to memoise a function that reads from a zipped file or URL and returns a value.

What I've tried so far (inspired from other CRAN packages):

  1. add memoise under Imports.
    usethis::use_package("memoise")

2 )Define a function like this:

#' Title
#'
#' @param object value
#'
#' @return value
#' @export
hello <- memoise::memoise(function(object) {
  object + 1
})
  1. Check and Build the package with Rstudio

  2. Check if function is memoised
    > memoise::is.memoised(hello)
    [1] TRUE

  3. Run an example
    > hello(2)
    [1] 3

  4. Check if result is cached:
    > memoise::has_cache(hello)(2)
    [1] FALSE

So is.memoise reports the function as memoised, but it doesn't seem to cache the result.

What's the correct way of memoising a function in package in this way?

@jimhester
Copy link
Member

I am pretty sure this works at least for the CRAN version of memoise, perhaps there is a bug in the devel version that is preventing it however.

@JosiahParry
Copy link

JosiahParry commented Sep 11, 2024

I too am running into this issue. Is there a vignette on using memoise with a package's function?

Edit: I believe this might be because the function that I was calling had additional function calls inside of it that themselves were not memoised. I memoised additional functions that were being called and that has seemingly resolved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants