Skip to content

Commit

Permalink
restore supporting data.table not inheriting from data.frame (#5210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle committed Oct 11, 2021
1 parent 92827d9 commit a41ae4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -2527,8 +2527,10 @@ copy = function(x) {
}

shallow = function(x, cols=NULL) {
if (!is.data.frame(x))
if (!is.data.frame(x) && !is.data.table(x)) {
# ^^ some revdeps do class(x)="data.table" without inheriting from data.frame, PR#5210
stopf("x is not a data.table|frame. Shallow copy is a copy of the vector of column pointers (only), so is only meaningful for data.table|frame")
}
ans = .shallow(x, cols=cols, retain.key=selfrefok(x)) # selfrefok for #5042
ans
}
Expand Down
7 changes: 6 additions & 1 deletion inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ test(211, ncol(TESTDT), 2L)
DT = data.table(a=1:6,key="a")
test(212, DT[J(3)]$a, 3L) # correct class c("data.table","data.frame")
class(DT) = "data.table" # incorrect class, but as from 1.8.1 it works. By accident when moving from colnames() to names(), it was dimnames() doing the check, but rather than add a check that identical(class(DT),c("data.frame","data.table")) at the top of [.data.table, we'll leave it flexible to user (user might not want to inherit from data.frame for some reason).
test(213, DT[J(3)]$a, error="x is not a data.table|frame") # from v1.14.2, data.table must inherit from data.frame (internals are too hard to reason if a data.table may not be data.frame too)
test(213, DT[J(3)]$a, 3L)

# setkey now auto coerces double and character for convenience, and
# to solve bug #953
Expand Down Expand Up @@ -18266,3 +18266,8 @@ test(testnum+0.01, DT[, prod(l), g], error="GForce prod can only be applied to c
# tables() error when called from inside a function(...), #5197
test(2221, (function(...) tables())(), output = "No objects of class data.table exist")

# some revdeps do class(x)="data.table" without inheriting from data.frame, PR#5210
DT = data.table(A=1:3)
class(DT) = "data.table"
test(2222, print(DT), output="A.*3")

0 comments on commit a41ae4a

Please sign in to comment.