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

custom data.frame classes redirected to as.data.frame before as.data.table #5700

Merged
merged 8 commits into from
Jan 17, 2025
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))
jangorecki marked this conversation as resolved.
Show resolved Hide resolved
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)