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

setDT generates shallow copy earlier to avoid interfering with attributes of co-bound tables #6551

Merged
merged 12 commits into from
Oct 1, 2024
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ rowwiseDT(

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.

14. `setDT()` no longer modifies the class of other names bound to the origin data.frame - e.g., argument DFs names at a caller to a function which uses setDT. Cf [#4784](https://github.com/Rdatatable/data.table/issues/4784). Thanks @OfekShilon for the report and fix.

## NOTES

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.
Expand Down
6 changes: 6 additions & 0 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -2922,12 +2922,18 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
break
}
}

# setalloccol results in a shallow copy. Must be performed before class setting not to affect other copies of x (#4784)
x = .shallow(x)

rn = if (!identical(keep.rownames, FALSE)) rownames(x) else NULL
setattr(x, "row.names", .set_row_names(nrow(x)))
if (check.names) setattr(x, "names", make.names(names(x), unique=TRUE))

# fix for #1078 and #1128, see .resetclass() for explanation.
setattr(x, "class", .resetclass(x, 'data.frame'))
setalloccol(x)

if (!is.null(rn)) {
nm = c(if (is.character(keep.rownames)) keep.rownames[1L] else "rn", names(x))
x[, (nm[1L]) := rn]
Expand Down
8 changes: 8 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -20583,3 +20583,11 @@ test(2294.72,
character(0)),
label = list(character = "C3", VCharA = "Total", integer = 2L))),
warning = "For the following variables, the 'label' value was already in the data: [VCharB (label: C3), VIntA (label: 2)]")

# setDT no longer leaks class modification to origin copy, #4784
d1 = data.frame(a=1, row.names='b')
d2 = d1
setDT(d2)
test(2295.1, !is.data.table(d1))
test(2295.2, rownames(d1), 'b')
test(2295.3, is.data.table(d2))
Loading