Skip to content

Commit

Permalink
Closes #476, #825. names<-.data.table fix for v3.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsrinivasan committed Oct 3, 2014
1 parent ee53eab commit 74f1f3a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,7 @@ dimnames.data.table = function(x) {
caller = as.character(sys.call(-2L))[1L]
if ( ((tt<-identical(caller,"colnames<-")) && cedta(3)) ||
cedta() ) warning("The ",if(tt)"col","names(x)<-value syntax copies the whole table. This is due to <- in R itself. Please change to setnames(x,old,new) which does not copy and is faster. See help('setnames'). You can safely ignore this warning if it is inconvenient to change right now. Setting options(warn=2) turns this warning into an error, so you can then use traceback() to find and change your ",if(tt)"col","names<- calls.")
else x = shallow(x) ## Fix for #476 and #825. Needed for R v3.1.0+
if (is.null(value))
setattr(x,"names",NULL) # e.g. plyr::melt() calls base::unname()
else
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

In both these cases (and during a `not-join` which was already fixed in [1.9.4](https://github.com/Rdatatable/data.table/blob/master/README.md#bug-fixes-1)), `allow.cartesian` can be safely ignored.

4. `names<-.data.table` works as intended on data.table unaware packages with Rv3.1.0+. Closes [#476](https://github.com/Rdatatable/data.table/issues/476) and [#825](https://github.com/Rdatatable/data.table/issues/825). Thanks to ezbentley for reporting [here](http://stackoverflow.com/q/23256177/559784) on SO and to @narrenfrei.

#### NOTES


Expand Down
19 changes: 19 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if (!exists("test.data.table",.GlobalEnv,inherits=FALSE)) {
require(bit64)
require(gdata)
require(GenomicRanges)
require(caret)
})
# The suppression is for users running test.data.table(). If they don't have ggplot2 installed we don't
# want to warn about it not being installed. 'R CMD check' is a stricter test that will warn if the
Expand Down Expand Up @@ -5279,6 +5280,24 @@ test(1383.1, DT[letters[1:3], list(var)], DT[1:5, list(var)])
DT=data.table(id=c(1,1), date=c(1992,1991), value=c(4.1,4.5), key="id")
test(1383.2, copy(DT)[DT, a:=1], DT[, a := 1])

# Fix for #476 and #825
if ("package:reshape" %in% search()) {
DT <- data.table(ID = c(611557L, 611557L, 611557L, 894125L, 894125L, 894125L, 894125L, 894125L, 898856L, 898856L, 898856L, 898856L, 898856L, 898856L, 898899L, 898899L, 898899L), DATUM = structure(c(16101, 16071, 16261, 16104, 16133, 16167, 16201, 16236, 16089, 16118, 16147, 16176, 16236, 16208, 16163, 16125, 16209), class = "Date"), N = c(25L, 9L, 23L, 29L, 26L, 26L, 27L, 28L, 39L, 39L, 38L, 36L, 40L, 39L, 19L, 20L, 19L), rank = c(2, 1, 3, 1, 2, 3, 4, 5, 1, 2, 3, 4, 6, 5, 2, 1, 3))
ans = cast(DT, ID ~ rank, value = "DATUM")
test(1383.1, names(DT), c("ID", "DATUM", "N", "rank"))
} else {
cat("Tests 1383.1 not run. If required call library(reshape) first.\n")
}

if ("package:caret" %in% search()) {
DT <- data.table(x = rnorm(10), y = rnorm(10))
cv.ctrl <- trainControl(method = 'repeatedcv', number = 5, repeats = 1)
fit <- train(y ~ x, data = DT, 'lm', trControl = cv.ctrl)
test(1383.2, names(DT), c("x", "y"))
} else {
cat("Tests 1383.2 not run. If required call library(caret) first.\n")
}

##########################


Expand Down

0 comments on commit 74f1f3a

Please sign in to comment.