On Mon, 20 Feb 2017, stephen sefick wrote:

Hello,

I would like to add something to a data frame that is 1) invisible to the
user, 2) has no side effects, and 3) I can test for in a following
function. Is this possible? I am exploring classes and attributes and I
have thought about using a list (but 1 and 2 not satisfied). Any help would
be greatly appreciated.


Depends on exactly what you mean by `invisible' and `side effects'.

You can do this (but I am not necessarily recommending this):

add.stuff <- function(x,...){
+ class(x)<- c("more.stuff",class(x))
+ attr(x,"stuff")<- list(...)
+ x}


And printing and model functions will be unaffected:

df <- data.frame(a=1:3,b=letters[1:3])
df2 <- add.stuff(df,comment="wow", length="3 rows")
df2
  a b
1 1 a
2 2 b
3 3 c
attr(df2,"stuff")
$comment
[1] "wow"

$length
[1] "3 rows"

all.equal(lm(a~b,df),lm(a~b,df2)) # only call should differ
[1] "Component “call”: target, current do not match when deparsed"


And if you need some generics to take account of the "stuff" attribute, you can write the methods to do that.

---

Another solution is to put your data.framne in a package and then have other objects hold the 'stuff' stuff. Once your package is loaded or imported, the user will have access to the data in a way that might be said to be `invisible' in ordinary usage.

---

But seriously, you should say *why* you want to do this. There are probably excellent solutions that do not involve directly altering the data.frame and may not involve putting together a package.

HTH,

Chuck
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to