R: Pass by reference

Posted by Pierre on Stack Overflow See other posts from Stack Overflow or by Pierre
Published on 2010-04-08T20:23:37Z Indexed on 2010/04/08 21:33 UTC
Read the original article Hit count: 720

Can you pass by reference with "R" ? for example, in the following code:

setClass("MyClass",
    representation(
    name="character"
    ))


instance1 <-new("MyClass",name="Hello1")
instance2 <-new("MyClass",name="Hello2")

array = c(instance1,instance2)

instance1
array

instance1@name="World!"

instance1
array

the output is

> instance1
An object of class “MyClass”
Slot "name":
[1] "World!"

> array
[[1]]
An object of class “MyClass”
Slot "name":
[1] "Hello1"


[[2]]
An object of class “MyClass”
Slot "name":
[1] "Hello2"

but I wish it was

> instance1
An object of class “MyClass”
Slot "name":
[1] "World!"

> array
[[1]]
An object of class “MyClass”
Slot "name":
[1] "World!"


[[2]]
An object of class “MyClass”
Slot "name":
[1] "Hello2"

is it possible ?

Thanks

Pierre

© Stack Overflow or respective owner

Related posts about r

    Related posts about argument-passing