Can I create an S4 superclass of "function" and access the slots of that object from the function call? At the moment I have:
> setClass("pow",representation=representation(pow="numeric"),contains="function") [1] "pow" > z=new("pow",function(x){x^2},pow=3) > z(2) [1] 4 Now what I really want is for the function to be x to the power of the @pow slot of itself, so if I then do:
> z@pow=3 I get cubes, and if I do:
> z@pow=2 I get squares.
But I don't see how to get a reference to 'self' like I would do in Python. I'm guessing its somewhere in the environment somewhere...
Here's how it works in python:
class Pow: def __init__(self,power): self.power=power self.__call__ = lambda x: pow(x,self.power) p = Pow(2) # p is now a 'squarer' print p(2) # prints 4 p.power=3 # p is now a 'cuber' print p(2) # prints 8 Couldn't really be easier, and I didn't even have to do "import antigravity"....