【发布时间】:2011-02-05 21:17:26
【问题描述】:
你能通过引用传递 "R" 吗? 例如,在以下代码中:
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
输出是
> 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"
但我希望是这样
> 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"
有可能吗?
【问题讨论】:
-
我真的很想知道他们为什么想出这样一个不寻常的实现。
-
对象还是原语? S3、S4 还是 R6?使用环境还是其他方式? R 1.x、2.x 还是 3.x?这里的答案跨越 2010-15 年,他们彼此不同意。这个问题是一团糟,需要清理。此外,在说“是/否”来引用版本或日期时很有用:例如'截至 R 3.0 / 2013'。让答案永不过时。
标签: r oop parameter-passing pass-by-reference