S4对象的原位修改:赞成和缺点

时间:2015-07-19 21:19:42

标签: r s4

使用eval.parentsubstitute,可以编写一个修改其参数的函数;例如:

> setClass("foo", representation(bar="vector"))
> f <- function(x) eval.parent(substitute(x@bar <- x@bar+1))
> a <- new("foo", bar = 0)
> a
An object of class "foo"
Slot "bar":
[1] 0

> f(a)
> a
An object of class "foo"
Slot "bar":
[1] 1

然而,大多数时候人们更喜欢写如下:

> g <- function(x) { x@bar <- x@bar + 1; x }
> a <- g(a)
> a
An object of class "foo"
Slot "bar":
[1] 2

第一种方法有问题吗?

在提交给CRAN的包中使用它是否可以(check --as-cran没有警告,所以答案应该是肯定的)?

感谢您的想法。

0 个答案:

没有答案