Skip to content

Commit b4538a0

Browse files
setDT generates shallow copy earlier to avoid interfering with attributes of co-bound tables (#6551)
* Fix #4784: setDT inside a function no longer modifies the class of the data.frame argument * data.table style * Add a second, "dumb" test for completeness * Run setalloccol before _any_ set* affects original table * New test of row.names issue for moving setalloccol up * Directly call .shallow to keep selfrefok() in other tests * trailing ws * NEWS wording & improvement * Improve comment wording * Shrink ws diff --------- Co-authored-by: Ofek Shilon <[email protected]>
1 parent d62aac9 commit b4538a0

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ rowwiseDT(
111111

112112
13. Restore some join operations on `x` and `i` (e.g. an anti-join `x[!i]`) where `i` is an extended data.frame, but not a data.table (e.g. a `tbl`), [#6501](https://github.com/Rdatatable/data.table/issues/6501). Thanks @MichaelChirico for the report and PR.
113113

114+
14. `setDT()` no longer modifies the class of other names bound to the origin data.frame, e.g., in `DF1 <- data.frame(a=1); DF2 <- DF1; setDT(DF2)`, `DF1`'s class will not change. [#4784](https://github.com/Rdatatable/data.table/issues/4784). Thanks @OfekShilon for the report and fix.
115+
114116
## NOTES
115117
116118
1. Tests run again when some Suggests packages are missing, [#6411](https://github.com/Rdatatable/data.table/issues/6411). Thanks @aadler for the note and @MichaelChirico for the fix.

R/data.table.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,10 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
29222922
break
29232923
}
29242924
}
2925+
2926+
# Done to avoid affecting other copies of x when we setattr() below (#4784)
2927+
x = .shallow(x)
2928+
29252929
rn = if (!identical(keep.rownames, FALSE)) rownames(x) else NULL
29262930
setattr(x, "row.names", .set_row_names(nrow(x)))
29272931
if (check.names) setattr(x, "names", make.names(names(x), unique=TRUE))

inst/tests/tests.Rraw

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20583,3 +20583,11 @@ test(2294.72,
2058320583
character(0)),
2058420584
label = list(character = "C3", VCharA = "Total", integer = 2L))),
2058520585
warning = "For the following variables, the 'label' value was already in the data: [VCharB (label: C3), VIntA (label: 2)]")
20586+
20587+
# setDT no longer leaks class modification to origin copy, #4784
20588+
d1 = data.frame(a=1, row.names='b')
20589+
d2 = d1
20590+
setDT(d2)
20591+
test(2295.1, !is.data.table(d1))
20592+
test(2295.2, rownames(d1), 'b')
20593+
test(2295.3, is.data.table(d2))

0 commit comments

Comments
 (0)