Skip to content

Commit

Permalink
custom data.frame classes redirected to as.data.frame before as.data.…
Browse files Browse the repository at this point in the history
…table, #5699
  • Loading branch information
jangorecki committed Oct 4, 2023
1 parent 8803918 commit 6de2742
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/as.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6de2742

Please sign in to comment.