[Rd] Accessing an object from the function in its .Data slot?

luke at stat.uiowa.edu luke at stat.uiowa.edu
Tue Jun 23 16:46:37 CEST 2009


sys.function might be useful.

luke

On Tue, 23 Jun 2009, Duncan Murdoch wrote:

> Martin Kober wrote:
>> I have an S4 object extending "function". Is it there a good way to
>> access the object (resp. its slots) from within the function in .Data?
>> 
>
> I don't think so.  That's not really the way the S4 object system is designed 
> to work.  If a function needs access to an object, the object should be one 
> of the args to the function, or defined in its environment.  Where the 
> function happens to be stored is irrelevant.
>
> Duncan Murdoch
>> If I'm reading help files correctly, it is not possible to overload
>> '(' (which would be the most elegant solution, I suppose).  I can
>> enclose the slotted data in the function when it is created, but that
>> obviously doesn't work when that data changes later on (also it
>> duplicates the data).
>> 
>> I tried to use the call stack, and this does seem to work:
>>
>> 
>>> setClass("abcd", representation(.Data="function", x="character"))
>>> a = new("abcd",
>>>     .Data=function() str( get( as.character( sys.call()[[1]] ), 
>>> pos=parent.frame() ) ),
>>>     x="testtxt")
>>> a()
>>> 
>> Formal class 'abcd' [package ".GlobalEnv"] with 2 slots
>>   ..@ .Data:function (x)
>>   ..@ x    : chr "testtxt"
>> 
>> That is obviously a not-so-elegant hack, and I'm not sure it works
>> under all circumstances as I'm not an expert on R's call system.
>> 
>> Is there any better way to achieve this?
>> 
>> Thanks in advance
>> Martin
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list