memoise icon indicating copy to clipboard operation
memoise copied to clipboard

Missing example for `...` parameter to `memoise`

Open kent37 opened this issue 4 years ago • 1 comments

The documentation for the ... parameter for memoise says to "See Examples for usage." There don't seem to be any usage examples here. The parameter is used in the example for timeout but there is no additional explanation of how to use it. https://github.com/r-lib/memoise/blob/3b0bf3e5fd9239c1dbde5fb2534d61ae5c6c8acf/R/memoise.R#L56

kent37 avatar Feb 22 '22 16:02 kent37

I was being curious on the same topic.
It this how it should work?

The ... are additional parameters that may invalidate the cache when they change their values. These are evaluated when the memoised function is called (hence the reason for supplying it as a formula).
In this case, the argument dt changes value every 3 seconds.

library(memoise)

a <- function(n) { runif(n) }
memA <- memoise(a, dt = ~timeout(3))


memA(3)
#> [1] 0.7704398 0.2082827 0.1184781
memA(3)
#> [1] 0.7704398 0.2082827 0.1184781
memA(3)
#> [1] 0.7704398 0.2082827 0.1184781
Sys.sleep(5)
memA(3)
#> [1] 0.38938505 0.06540492 0.72489978
memA(3)
#> [1] 0.38938505 0.06540492 0.72489978

Created on 2024-01-18 with reprex v2.1.0

Thanks!

lgaborini avatar Jan 18 '24 10:01 lgaborini