You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 tibblesmpg2<- 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
Adding such method means extending dependencies of DT. We rather would like to reduce them, also Suggested dependencies. In this case there are many recursive dependencies as well.
Is there a reason why as_tibble.data.frame method doesn't work?
going via data.frame should work after looking at as.data.frame.data.table and what is under
Isn't this the Enhances: dependency, and isn't the direction reversed, i.e., {data.table} would provide as.data.table.tibble() and {tibble} would provide as_tibble.data.table()?
if as_tibble would go via as.data.frame then all should work seamlessly, but it must be dispatching to second class attribute "data.frame" instead.
Defining the following should be sufficient
because as.data.frame.data.table method handles removing its attributes.
I believe this problem is not only related to data.table but any package that defines class extending data.frame and uses custom attributes. as_tibble will assume it is standard data.frame and not go via as_tibble.default. Therefore to resolve problem at the root it should be rather something like that
as_tibble.data.frame=function(x) {
if (!identical(class(x), "data.frame")) return(as_tibble(as.data.frame(x)))
...# continue old as_tibble.data.frame body
}
I noticed data.table would suffer from the same issue as tibble so I filled #5699 so problem can be addressed nicely without needing any package to add dependencies.
@jangorecki - do you want to raise this suggestion over at {tibble}? (I'm happy to do so if you'd prefer not to but didn't want to jump in over the top).
Activity
jangorecki commentedon Oct 4, 2023
Adding such method means extending dependencies of DT. We rather would like to reduce them, also Suggested dependencies. In this case there are many recursive dependencies as well.
Is there a reason why as_tibble.data.frame method doesn't work?
going via data.frame should work after looking at as.data.frame.data.table and what is under
data.table/R/data.table.R
Line 2799 in 8803918
I believe there reason is presented by this minimal example, which doesn't need tibble to reproduce:
MichaelChirico commentedon Oct 4, 2023
Isn't this the
Enhances:dependency, and isn't the direction reversed, i.e., {data.table} would provideas.data.table.tibble()and {tibble} would provideas_tibble.data.table()?jangorecki commentedon Oct 4, 2023
So the problem is that initial dataset to which you want to compare to isn't just data.frame. If we use normal data.frame it works as expected.
if
as_tibblewould go viaas.data.framethen all should work seamlessly, but it must be dispatching to second class attribute "data.frame" instead.Defining the following should be sufficient
because as.data.frame.data.table method handles removing its attributes.
I believe this problem is not only related to data.table but any package that defines class extending data.frame and uses custom attributes.
as_tibblewill assume it is standard data.frame and not go viaas_tibble.default. Therefore to resolve problem at the root it should be rather something like thatI noticed data.table would suffer from the same issue as tibble so I filled #5699 so problem can be addressed nicely without needing any package to add dependencies.
jangorecki commentedon Oct 4, 2023
Indeed proposed one line change works as expected: https://github.com/Rdatatable/data.table/pull/5700/files
TimTaylor commentedon Oct 25, 2023
@jangorecki - do you want to raise this suggestion over at {tibble}? (I'm happy to do so if you'd prefer not to but didn't want to jump in over the top).
Fixes tidyverse#1556
AngelFelizR commentedon Nov 4, 2023
Thanks @TimTaylor
@ccsarapas I think that this issue is ready to be closed.
Fixes tidyverse#1556