[R] Does anyone have any use for this?

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri Jan 1 21:20:00 CET 2021

Hi all:

In the course of playing around with other issues, I wrote the following
little function that allows functions to keep state
and easily replay and update their state.(see comments after):

memify <- function(f)
   if (is.primitive(f)) {
      cat("Cannot memify primitive functions.\n")
   if (!inherits(f, "function"))
      stop("Argument must inherit from class 'function'")
   arglist <- list()
      function(...) {
         m <- tryCatch(
            error = function(e) {
               warning("Bad function call; cannot update arguments\n")
         nm <- names(m)
         hasname <- nm != "" #logical index of named values
         if (any(hasname)) {
            if (anyDuplicated(nm, incomparables = ""))
               warning("Duplicated names in call; only the first will be
            arglist <<- modifyList(arglist, m[hasname]) ## this is what
does the job
         do.call(f, modifyList(m, arglist))
      class = c("memified", class(f)))


 x <- 1:9; y <- runif(9)
 plt <- memify(plot)
 x <- 1:9; y <- runif(9)
 plt(x,y, col = "blue")  ## plt "remembers" these arguments; i.e. keeps
 plt( type = "b") ## all other arguments as previous
 plt(col = "red") ## ditto

So my question is: Beyond allowing one to easily change/add argument values
and replay when there are lots of arguments floating around, which we often
use an IDE's editor to do, is there any real use for this? I confess that,
like Pygmalion, I have been charmed by this idea, but it could well be
useless, By all means feel free to chastise me if so.

1. I am aware that many functions already have "update" methods to "update"
their results without re-entering all arguments -- e.g. lattice graphics,
glm, etc.
2. Several packages -- rlang and R6 anyway -- but very likely more, do this
sort of thing and way more; the price is added complexity, of course.
3. I realize also that keeping state would be a bad idea in many
circumstances, e.g. essentially changing documented defaults.

Reply privately to praise or bury if you do not think this is of any
interest to readers of this list. Publicly is fine, too. If it's dumb it's

Cheers and best wishes for a better new year for us all,

Bert Gunter

	[[alternative HTML version deleted]]

More information about the R-help mailing list