tidyverse / tibble

A modern re-imagining of the data frame
https://tibble.tidyverse.org/
Other
671 stars 130 forks source link

`as_tibble.data.table()` method? #1555

Closed ccsarapas closed 1 year ago

ccsarapas commented 1 year ago

Would you consider adding a data.table method to as_tibble()? On extremely cursory testing, it may be as simple as adapting as_tibble.data.frame() with an extra line to remove the .internal.selfref attribute:

library(tibble)
library(data.table)
data(mpg, package = "ggplot2")

# two identical tibbles
mpg2 <- copy(mpg)
identical(mpg, mpg2)
#> [1] TRUE

# after converting to data.table and back to tibble, no longer identical
setDT(mpg2)
mpg2 <- as_tibble(mpg2)
identical(mpg, mpg2)
#> [1] FALSE

# identical again after removing ".internal.selfref" attribute
attr(mpg2, ".internal.selfref") <- NULL
identical(mpg, mpg2)
#> [1] TRUE

Created on 2023-09-22 with reprex v2.0.2

krlmlr commented 1 year ago

Thanks. In my opinion, this should be the responsibility of the data.table package. This package can implement an as_tibble.data.table() method without pulling in a strong dependency on tibble.

@hadley: Where do we document which packages should implement which methods? I haven't found it in https://design.tidyverse.org/ . Should this be a chapter or a paragraph somewhere?

jennybc commented 1 year ago

R Packages has some coverage of these matters:

https://r-pkgs.org/dependencies-in-practice.html#imports-and-exports-related-to-s3

... An add-on package that implements an S3 generic for a new class should list the generic-providing package in Imports, import the generic into its namespace, and export its S3 method....

Which does support your proposal that an as_tibble() method for data.table would naturally be implemented by the owner of the data.table class. The section goes on to talk about conditional registration, if you list the generic-owning package in Suggests instead of Imports, and s3_register().

krlmlr commented 1 year ago

Thanks, @jennybc, for the pointer!

@ccsarapas: Can you please take this to the data.table issue tracker if needed?

github-actions[bot] commented 3 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.