Skip to content

Commit fa16740

Browse files
committed
force class reset
1 parent 0909048 commit fa16740

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

R/as.data.table.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,11 @@ as.data.table.list = function(x,
215215

216216
as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
217217
if (is.data.table(x)) return(as.data.table.data.table(x, key=key)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
218-
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x), keep.rownames=keep.rownames, key=key, ...))
218+
if (!identical(class(x), "data.frame")) {
219+
x = as.data.frame(x)
220+
setattr(x,"class","data.frame") # cater for cases when as.data.frame is overridden #6874
221+
return(as.data.table(x, keep.rownames=keep.rownames, key=key, ...))
222+
}
219223
if (!isFALSE(keep.rownames)) {
220224
# can specify col name to keep.rownames, #575; if it's the same as key,
221225
# kludge it to 'rn' since we only apply the new name afterwards, #4468

inst/tests/tests.Rraw

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21104,3 +21104,8 @@ DT = data.table(a = 1:5, b = 1:5, x = 1:5)
2110421104
test(2309.06, key(as.data.table(DT, key="a")), "a")
2110521105
test(2309.07, key(as.data.table(DT)), NULL)
2110621106
test(2309.08, key(as.data.table(DT, key=NULL)), NULL)
21107+
21108+
# as.data.frame(x) does not reset class(x) to "data.frame" #6874
21109+
as.data.frame.no.reset = function(x) x
21110+
DF = structure(list(a = 1:2), class = c("data.frame", "no.reset"), row.names = c(NA, -2L))
21111+
test(2310.01, as.data.table(DF), data.table(a=1:2))

0 commit comments

Comments
 (0)