diff --git a/R/data.table.R b/R/data.table.R index ec5fb63cd1..3f57aec956 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -2482,7 +2482,7 @@ setDF <- function(x, rownames=NULL) { invisible(x) } -setDT <- function(x, keep.rownames=FALSE, key=NULL) { +setDT <- function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) { name = substitute(x) if (is.name(name)) { home <- function(x, env) { @@ -2501,10 +2501,12 @@ setDT <- function(x, keep.rownames=FALSE, key=NULL) { # fix for #1078 and #1128, see .resetclass() for explanation. setattr(x, 'class', .resetclass(x, 'data.table')) if (!missing(key)) setkeyv(x, key) # fix for #1169 + if (check.names) setattr(x, "names", make.names(names(x), unique=TRUE)) if (selfrefok(x) > 0) return(invisible(x)) else alloc.col(x) } else if (is.data.frame(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')) alloc.col(x) @@ -2530,6 +2532,7 @@ setDT <- function(x, keep.rownames=FALSE, key=NULL) { xn[idx] = paste("V", seq_along(which(idx)), sep="") setattr(x, "names", xn) } + if (check.names) setattr(x, "names", make.names(xn, unique=TRUE)) } setattr(x,"row.names",.set_row_names(max(n))) setattr(x,"class",c("data.table","data.frame")) diff --git a/README.md b/README.md index 43c63cea01..6f20f71a8e 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ 24. `[.data.table` now accepts single column numeric matrix in `i` argument the same way as `data.frame`. Closes [#826](https://github.com/Rdatatable/data.table/issues/826). Thanks to @jangorecki for the PR. + 25. `setDT()` gains `check.names` argument paralleling that of `fread`, `data.table`, and `base` functionality, allowing poorly declared objects to be converted to tidy `data.table`s by reference. Closes [#1338](https://github.com/Rdatatable/data.table/issues/1338); thanks to @MichaelChirico for the FR/PR. + #### BUG FIXES 1. `if (TRUE) DT[,LHS:=RHS]` no longer prints, [#869](https://github.com/Rdatatable/data.table/issues/869) and [#1122](https://github.com/Rdatatable/data.table/issues/1122). Tests added. To get this to work we've had to live with one downside: if a `:=` is used inside a function with no `DT[]` before the end of the function, then the next time `DT` or `print(DT)` is typed at the prompt, nothing will be printed. A repeated `DT` or `print(DT)` will print. To avoid this: include a `DT[]` after the last `:=` in your function. If that is not possible (e.g., it's not a function you can change) then `DT[]` at the prompt is guaranteed to print. As before, adding an extra `[]` on the end of a `:=` query is a recommended idiom to update and then print; e.g. `> DT[,foo:=3L][]`. Thanks to Jureiss and Jan Gorecki for reporting. diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b53dd4f3ec..2af6f465b0 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -6956,6 +6956,15 @@ ans = data.table(AAA=as.integer(c(4,7,rep(1,17),31,21)), CCC=as.integer(c(6,9,rep(3,17),33,23))) test(1558, fread(f, nrow=21L), ans) # no warning +# FR # 1338 -- check.names argument of setDT +ans=data.table(X=1:3,"X.1"=1:3) +dt1<-data.table(X=1:3,X=1:3) +df1<-data.frame(X=1:3,X=1:3,check.names=FALSE) +ls1<-list("X"=1:3,"X"=1:3) +test(1559.1, setDT(dt1, check.names=TRUE), ans) +test(1559.1, setDT(df1, check.names=TRUE), ans) +test(1559.1, setDT(ls1, check.names=TRUE), ans) + ##########################