Hi Abby,
Something along the line of:
setClass("S4Function",
contains="function",
representation(name="character", more_stuff="ANY")
)
seems to do what you want:
f <- new("S4Function", function(a) a^2, name="square")
# 'f' is both an S4 object and a function:
is.object(f)
# [1] TRUE
is.function(f)
# [1] TRUE
f@name
# [1] "square"
f(11)
# [1] 121
Hope this helps,
H.
On 2/12/21 1:53 PM, Abby Spurdle (/əˈbi/) wrote:
Dear All,
I was wondering if it's possible to create S4 function objects?
(Or S4 closures, if you prefer).
i.e.
An R object, that is both an S4 object, and a function.
This would allow one to write:
f <- constructor4.functionobj ()
f ()
f...@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 ()
t...@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.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
--
Hervé Pagès
Bioconductor Core Team
hpages.on.git...@gmail.com
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.