From marc_schwartz at me.com  Fri Jul  9 15:52:56 2010
From: marc_schwartz at me.com (Marc Schwartz)
Date: Fri, 09 Jul 2010 08:52:56 -0500
Subject: [Rd] Defining a method that behaves like '$'?
In-Reply-To: <4C371FB0.9040700@cbio.uct.ac.za>
References: <4C371398.80603@cbio.uct.ac.za> <4C371645.6030204@gmail.com>
	<4C371AE3.9060801@cbio.uct.ac.za>
	<8D37932C-62F7-44C0-BAFA-9A77ABADE59F@me.com>
	<4C371FB0.9040700@cbio.uct.ac.za>
Message-ID: <C9D4442C-E56E-43D6-B165-A6228F40B98B@me.com>

I am certainly not an expert in S4 and know enough to be dangerous.

That being said, I used setMethod() as Romain had done. I would defer to others with greater experience as to the pros and cons, including the risk of confusion, but here is one approach, extending the example in ?slot:

setClass("track", representation(x = "numeric", y = "numeric", z = "list"))

myTrack <- new("track", x = -4:4, y = exp(-4:4), z = list(A = 1:5, B = 7:12))

setMethod( "$", "track", function(x, name){
	slot(x, "z")[[name]]
})


> myTrack
An object of class "track"
Slot "x":
[1] -4 -3 -2 -1  0  1  2  3  4

Slot "y":
[1]  0.01831564  0.04978707  0.13533528  0.36787944  1.00000000
[6]  2.71828183  7.38905610 20.08553692 54.59815003

Slot "z":
$A
[1] 1 2 3 4 5

$B
[1]  7  8  9 10 11 12



> myTrack at z
$A
[1] 1 2 3 4 5

$B
[1]  7  8  9 10 11 12



# Default
> myTrack at z$A
[1] 1 2 3 4 5

> myTrack at z$B
[1]  7  8  9 10 11 12



# Use the new '$' method
> myTrack$A
[1] 1 2 3 4 5

> myTrack$B
[1]  7  8  9 10 11 12


Not sure if that gets you something along the lines of what you wanted, but perhaps it is helpful.

Marc



On Jul 9, 2010, at 8:10 AM, Renaud Gaujoux wrote:

> I do not want to access the slot itself but its content: a:toto would be a at slot1[['toto']].
> The thing is that I would like to have two different methods: '$' (that I already have) and another one to define, ideally that behaves like '$'.
> So in brief:
> - a:toto would be for a at slot1[['toto']]
> - a$tata would be for a at slot2[['tata']]
> 
> But apparently it might not be possible.
> 
> -- 
> Renaud Gaujoux
> Computational Biology - University of Cape Town
> South Africa
> 
> 
> On 09/07/2010 14:58, Marc Schwartz wrote:
>> You were, in effect, trying to overload the ":" operator, which is of course for defining sequences.
>> 
>> If you are using S4 methods, what is wrong with using the default "@" as the extraction syntax (eg. a at name) to get at slots?
>> 
>> See ?"@" and ?slot
>> 
>> HTH,
>> 
>> Marc Schwartz
>> 
>> On Jul 9, 2010, at 7:49 AM, Renaud Gaujoux wrote:
>> 
>>   
>>> Alright.
>>> Maybe the symbol I chose was not appropriate. I tried ':' to be able to do 'a:name' with 'a' a S4 object.
>>> I get the following error:
>>> Error in genericForPrimitive(f) :
>>>  methods may not be defined for primitive function ":" in this version of R
>>> 
>>> Does there exist any symbol that would be suitable for the job?
>>> Thanks
>>> 
>>> -- 
>>> Renaud Gaujoux
>>> Computational Biology - University of Cape Town
>>> South Africa
>>> 
>>> 
>>> On 09/07/2010 14:29, Duncan Murdoch wrote:
>>>     
>>>> On 09/07/2010 8:18 AM, Renaud Gaujoux wrote:
>>>>       
>>>>> Hi,
>>>>> 
>>>>> is there a way to define a method say '$$' that would behave like '$' and allow calls like 'a$$name'?
>>>>>         
>>>> No, the parser handles a fixed syntax, and that expression is not legal.  You could do it with
>>>> 
>>>> a %$$% name
>>>> 
>>>> using the infix operator syntax.  (I think the description in the R Language Definition suggests this is not legal, since $$ is not a valid name, but it does currently work and that's unlikely to change.)
>>>> 
>>>> Duncan Murdoch
>>>>       
>>



[Rd] Defining a method that behaves like '$'?
Marc Schwartz