Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alter class of data.table object #839

Closed
jangorecki opened this issue Sep 29, 2014 · 4 comments
Closed

alter class of data.table object #839

jangorecki opened this issue Sep 29, 2014 · 4 comments
Labels

Comments

@jangorecki
Copy link
Member

I'm storing particular structure of data as data.table object. I would like to easily plot the object.

dt <- data.table(a = 1:10, b = rnorm(10), z = letters[1:10]) # my custom data stored as DT
plot.my_class <- function(x, ...) plot(x$a, x$b, type="l", main="my custom plot of custom object stored in DT")

Is it safe to alter class of data.table in the following way?

class(dt) <- c("my_class",class(dt))
plot(dt) # plot works fine

I would like to confirm is it safe the add another (my custom) class to DT object? will the rest of data.table functions works fine on my dt or maybe it is not recommended?

@rsaporta
Copy link
Contributor

Try to avoid using class(DT) <- ....

Instead use setattr(DT, "class", c("New_class", class(DT))

@arunsrinivasan
Copy link
Member

The idiomatic way would be to use setattr:

setattr(dt, c("myClass", class(dt)))

Why?

In R v3.1+, class() doesn't copy the entire table (like in previous versions). That's great!

But still, <-. in R results in data.table losing it's over allocation. Therefore, when you assign a column by reference with := or set the next time, you'll get a warning message that a shallow copy has been made to get the over-allocation back in track.

Try doing: data.table:::selfrefok(dt) before and after class(dt) <- ... to see the difference.

And this is not really an issue, so added the "question" tag. Feel free to re-open the issue if this doesn't answer (completely) (or if you've further questions).

@jangorecki
Copy link
Member Author

Yeah, my bad I've forgot about setattr. I fully agree it is not issue but only a question.
You focus more on my class() than on the quesiton itself.
But from your comment I assume there should be no any issues related to altered class of data.table object.
Thanks

@arunsrinivasan
Copy link
Member

Oh right. So to answer your questions specifically:

I would like to confirm is it safe the add another (my custom) class to DT object?

Yes, it's safe here. The downsides are: a) in versions of R <3.10, it'll copy the entire data.table, and b) in all versions of R, the over-allocation will be lost, which'll result in the warning explained above.

will the rest of data.table functions works fine on my dt or maybe it is not recommended?

Yes it will (and should). But the idiomatic way is setattr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants