R中的一类的访问属性 [英] access attributes of a class in R
本文介绍了R中的一类的访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个包含多个属性R创建的对象。
I have created an object in R that contains several attributes.
我怎样才能轻松地访问它们。
How can I easily access them.
我可以做
attr(x, attributeName)
或
attributes(x)$attributeName
但他们都不是太方便了。
but none of them is really convenient.
有没有更快的方法(如C ++中的点或Java)
Is there a quicker way (like the dot in C++ or java)
推荐答案
不要使用属性为对象,使用列表:
Don't use attributes for your object, use a list:
myobj <- structure(list(a = 1, b = 2), class = "myclass")
print.myclass <- function(x, ...) cat("A: ", x$a, " B: ", x$b, "\n", sep = "")
myobj
当然,这可能不是,如果你扩展现有对象(例如向量)工作,但在我的经验是建立一个对象通常更好的办法。
Of course, this might not work if you're extending an existing object (e.g. vector), but in my experience it's a generally better way to build objects.
这篇关于R中的一类的访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文