[R] Is there a diferent way to do this?

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Mar 28 18:19:10 CEST 2005


Gilvan Justino wrote:

> Hi,
>  
> Thanks for your reply.
>  
> I found a sample at http://www.angelfire.com/tx4/cus/shapes/r.html
>  
> I did the same with my class and now it is working
> ###
>  setGeneric("setName<-",
>             function(this, value)
>             standardGeneric("setName<-")
>             )
>  setReplaceMethod("setName", "Partri",
>            function(this, value)
>            {
>              this at name <- value
>              this
>            }
>            )
> ##
> I have other doubts:
> a) The usual representation of a method calling in other languages (I mean, Java, Delphi) is:
>    obj.method(<parameters>)

In S4 it is simple generic(object), the generic chooses the right method 
along the class of object.


> So, I believed that in R it was the same thing. Thats it, to call the method setName of my instance "x" I could write:
>    x.setName("blablabla")

Replacement methods are somehow special.
For the "regular" case, you say generic(object, otherArg1, otherArg2, 
......)

For a replacement method, you can also say
   "generic<-"(object, otherArg1, otherARg2, ...)

but for convenience and intuition, you can say

generic(object) <- otherArg1



> The right way really is? 
>      method(instance) <- parameters
> Thats it:
>      setName(x) <- "blablabla" ?

Yes, but as I already mentioned, replacement methods are somehow special.


> It sounds strange to me. Maybe because I used with other languages, but it doen´t seem legible to me this sintaxe.
>  
> b) I realised I have to use the name "value" as the parameter name, what else it won´t work. Is that corret?
>  
> c) and in the case I have two or more parameters. How should I do?

You don't want to do replacements in that way, do you?


> I typed ?setReplaceMethod in RGui how you suggested, but it doesn´t bring any information about this instruction.

I assumed (since your questions was quite complex) you are much more 
familar with R, sorry for causing confusion. I think the best way is to 
start reading some docs, not only about S4, but also about S3 classes, 
so you will see why things have been developed this way.

Uwe Ligges


> Thanks again and sorry for my english mistakes.
> Gilvan Justino
> 
> Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
> Gilvan Justino wrote:
> 
>>Hi,
>>
>>I started creating a small class but I'm courious about how it is working.
>>
>>To create a instance of my class "Partri" I write this at Rgui:
>>x <- new("Partri", name="Gilvan")
>>
>>and to change the slot "name" of this instance, I write:
>>setName(x, newname="Justino")
>>
>>It is not working, because when I type "x" at Rgui, it shows the initial value, which was "Gilvan". What I am doing wrong?
>>
>>I'll appreciate any king of help!
>>Gilvan
>>
>>
>># Classe para representar um parâmetro
>>setClass("Partri",
>># parâmetros
>>representation( name="character"),
>># seção de inicialização
>>prototype( name="undefined name" )
>>)
>>
>>setGeneric("setName",
>>function(object, newname,...)
>>standardGeneric("setName"))
>>setMethod("setName", 
>>signature(object="Partri",newname="character"), 
>>function(object, newname)
>>{
>>object at name = newname
> 
> 
> 
> You set the "name" in the environment of the method, but you don't 
> assign it to the environment that you seem to expect erroneously. To see 
> that it works, simply insert
> 
> print(object at name)
> 
> Instead, you want to write some non-standard replacement function. See 
> ?setReplaceMethod and friends (you might want to take a look into the 
> green book as well).
> 
> Uwe Ligges
> 
> 
> 
> 
> 
>>}
>>)
> 
> 
> 
> 
> 
>>
>>---------------------------------
>>
>>ora!
>>[[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 
> 
> __________________________________________________
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list