From 6de274277753f03ef6e59167e397438ccd04e930 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 4 Oct 2023 09:05:52 +0200 Subject: [PATCH] custom data.frame classes redirected to as.data.frame before as.data.table, #5699 --- R/as.data.table.R | 1 + inst/tests/tests.Rraw | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/R/as.data.table.R b/R/as.data.table.R index feae0f5ac..e8e5939c9 100644 --- a/R/as.data.table.R +++ b/R/as.data.table.R @@ -214,6 +214,7 @@ as.data.table.list = function(x, } as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) { + if (!identical(class(x), "data.frame")) return(as.data.frame(x)) if (!isFALSE(keep.rownames)) { # can specify col name to keep.rownames, #575; if it's the same as key, # kludge it to 'rn' since we only apply the new name afterwards, #4468 diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 9117c0fcb..4e87bacd4 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18094,3 +18094,15 @@ test(2238.6, "a" %notin% integer(), TRUE) test(2238.7, "a" %notin% NULL, TRUE) test(2238.8, NA %notin% 1:5, TRUE) test(2238.9, NA %notin% c(1:5, NA), FALSE) + +# as.data.table should remove extra attributes from extended data.frames #5699 +x = data.frame(a=c(1,5,3), b=c(2,4,6)) +class(x) = c("tbl","data.frame") +attr(x, "t1") = "a" +as.data.frame.tbl = function(x) { + attr(x, "t1") = NULL + class(x) = "data.frame" + x +} +y = as.data.table(x) +test(2239.1, attr(y, "t1", TRUE), NULL)