Skip to content

Commit

Permalink
Control "prevent.inplace" via an option, not a setDT argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
OfekShilon committed May 13, 2021
1 parent b88b3c8 commit 3ec3020
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 75 deletions.
14 changes: 12 additions & 2 deletions R/as.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ as.data.table.matrix = function(x, keep.rownames=FALSE, key=NULL, ...) {
for (i in ic) value[[i]] <- as.vector(x[, i]) # to drop any row.names that would otherwise be retained inside every column of the data.table
}
col_labels = dimnames(x)[[2L]]
setDT(value, set.allow.inplace.attrib=FALSE)

orig.opt <- options(data.table.prevent.inplace=TRUE)
on.exit(options(orig.opt))
setDT(value)

if (length(col_labels) == ncols) {
if (any(empty <- !nzchar(col_labels)))
col_labels[empty] = paste0("V", ic[empty])
Expand Down Expand Up @@ -196,7 +200,13 @@ as.data.table.list = function(x,
if (any(vnames==".SD")) stop("A column may not be called .SD. That has special meaning.")
if (check.names) vnames = make.names(vnames, unique=TRUE)
setattr(ans, "names", vnames)
setDT(ans, key=key, set.allow.inplace.attrib=FALSE) # copy ensured above; also, setDT handles naming

if (is.null(getOption("data.table.allow.inplace"))) { # not an internal call
orig.opt <- options(data.table.prevent.inplace=FALSE)
on.exit(options(orig.opt))
}

setDT(ans, key=key) # copy ensured above; also, setDT handles naming
if (length(origListNames)==length(ans)) setattr(ans, "names", origListNames) # PR 3854 and tests 2058.15-17
ans
}
Expand Down
13 changes: 9 additions & 4 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ null.data.table = function() {

data.table = function(..., keep.rownames=FALSE, check.names=FALSE, key=NULL, stringsAsFactors=FALSE)
{
if (is.null(getOption("data.table.prevent.inplace"))) {
# need to add an explicit option, for a nested call to as.list.data.table that calls setDT
orig.opt <- options(data.table.prevent.inplace=FALSE)
on.exit(options(orig.opt))
}

# NOTE: It may be faster in some circumstances for users to create a data.table by creating a list l
# first, and then setattr(l,"class",c("data.table","data.frame")) and forgo checking.
x = list(...) # list() doesn't copy named inputs as from R >= 3.1.0 (a very welcome change)
Expand Down Expand Up @@ -2708,8 +2714,7 @@ setDF = function(x, rownames=NULL) {
invisible(x)
}

setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE,
set.allow.inplace.attrib=TRUE) {
setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
name = substitute(x)
if (is.name(name)) {
home = function(x, env) {
Expand Down Expand Up @@ -2748,7 +2753,7 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE,
x[, (nm[1L]) := rn]
setcolorder(x, nm)
}
if(set.allow.inplace.attrib)
if(getOption("data.table.prevent.inplace", default=TRUE))
setattr(x, "allow.assign.inplace", FALSE)
} else if (is.list(x) && length(x)==1L && is.matrix(x[[1L]])) {
# a single list(matrix) is unambiguous and depended on by some revdeps, #3581
Expand Down Expand Up @@ -2785,7 +2790,7 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE,
setattr(x,"row.names",.set_row_names(n_range[2L]))
setattr(x,"class",c("data.table","data.frame"))
setalloccol(x)
if(set.allow.inplace.attrib)
if(getOption("data.table.prevent.inplace", default=TRUE))
setattr(x, "allow.assign.inplace", FALSE)
} else {
stop("Argument 'x' to 'setDT' should be a 'list', 'data.frame' or 'data.table'")
Expand Down
Loading

0 comments on commit 3ec3020

Please sign in to comment.