[R] Is it Possible to Create S4 Function Objects?

Abby Spurdle (/əˈbi/) @purd|e@@ @end|ng |rom gm@||@com
Fri Feb 12 22:53:27 CET 2021

Dear All,

I was wondering if it's possible to create S4 function objects?
(Or S4 closures, if you prefer).

An R object, that is both an S4 object, and a function.

This would allow one to write:

> f <- constructor4.functionobj ()
> f ()
> f using slot.of.f

I've searched for this, but I can't find any examples.

If it's possible, then that leads to the question of if/how the body
of f() could directly access the value of the slot?

> f <- function ()
>    this using slot.of.f

I should note that the more common approach of storing values in a
function's environment, doesn't work well for top-level objects.
This approach is dependent on immutability of the function's data, and
I'm reluctant to create such constraints.

More information about the R-help mailing list