Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ data.table <-function(..., keep.rownames=FALSE, check.names=FALSE, key=NULL, str
if (identical(x, list(NULL)) || identical(x, list(list())) ||
identical(x, list(data.frame(NULL))) || identical(x, list(data.table(NULL)))) return( null.data.table() )
nd = name_dots(...)
if (any(nocols<-sapply(x, NCOL)==0L)) { tt=!nocols; x=x[tt]; nd=lapply(nd,'[',tt); } # data.table(data.table(), data.table(a=integer())), #3445
myNCOL = function(x) if (is.null(x)) 0L else NCOL(x) # tmp fix (since NCOL(NULL)==1) until PR#3471 goes ahread in v1.12.4
if (any(nocols<-sapply(x, myNCOL)==0L)) { tt=!nocols; x=x[tt]; nd=lapply(nd,'[',tt); } # data.table(data.table(), data.table(a=integer())), #3445
vnames = nd$vnames
novname = nd$novname # novname used later to know which were explicitly supplied in the call
n <- length(x)
Expand All @@ -75,7 +76,7 @@ data.table <-function(..., keep.rownames=FALSE, check.names=FALSE, key=NULL, str
numcols = integer(n) # the ncols of each of the inputs (e.g. if inputs contain matrix or data.table)
for (i in seq_len(n)) {
xi = x[[i]]
if (is.null(xi)) stop("column or argument ",i," is NULL")
if (is.null(xi)) stop("Internal error: NULL item ", i," should have been removed from list above") # nocov
if ("POSIXlt" %chin% class(xi)) {
warning("POSIXlt column type detected and converted to POSIXct. We do not recommend use of POSIXlt at all because it uses 40 bytes to store one date.")
x[[i]] = as.POSIXct(xi)
Expand Down Expand Up @@ -1410,7 +1411,9 @@ replace_dot_alias <- function(e) {
setattr(jval, 'class', class(x)) # fix for #5296
if (haskey(x) && all(key(x) %chin% names(jval)) && suppressWarnings(is.sorted(jval, by=key(x)))) # TO DO: perhaps this usage of is.sorted should be allowed internally then (tidy up and make efficient)
setattr(jval, 'sorted', key(x))
for (i in seq_along(jval)) if (is.null(jval[[i]])) stop("Column ",i," of j evaluates to NULL. A NULL column is invalid.")
# postponed to v1.12.4 because package eplusr creates a NULL column and then runs setcolorder on the result which fails if there are fewer columns
# w = sapply(jval, is.null)
# if (any(w)) jval = jval[,!w,with=FALSE] # no !..w due to 'Undefined global functions or variables' note from R CMD check
}
return(jval)
}
Expand Down
13 changes: 9 additions & 4 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -13195,7 +13195,7 @@ test(1967.33, foverlaps(x, y, by.x = c('start', 'end'), by.y = c('end', 'start')
error = 'All entries in column end should be <= corresponding entries')

## data.table.R
test(1967.34, data.table(1:5, NULL), error = 'column or argument 2 is NULL')
test(1967.34, data.table(1:5, NULL), data.table(V1=1:5))
### testing branches:
### if (length(namesi)==0L) namesi = rep.int("",ncol(xi))
### if (any(tt)) namesi[tt] = paste0("V", which(tt))
Expand Down Expand Up @@ -13826,9 +13826,14 @@ DT = structure(list(NULL), names="a", class=c("data.table","data.frame"))
test(2009.1, DT[a>1], error="Column 1 is NULL; malformed data.table")
DT = null.data.table()
x = NULL
test(2009.2, DT[, .(x)], error="Column 1 of j evaluates to NULL. A NULL column is invalid.")
test(2009.3, data.table(character(0), NULL), error="column or argument 2 is NULL")
test(2009.4, as.data.table(list(y = character(0), x = NULL)), data.table(y=character()))
test(2009.2, DT[, .(x)], ans<-alloc.col(structure(list(x=NULL), names="x", class=c("data.table","data.frame"))))
# postponed to 1.12.4 ... V1=null.data.table()) # because .(x) evaluated to NULL; NULL columns in results removed
DT = data.table(A=1:3)
test(2009.3, DT[, .(x)], ans) # null.data.table()
test(2009.4, DT[, .(x, sum(A))], data.table(V1=6L))
test(2009.5, DT[, .(sum(A), x)], data.table(V1=6L))
test(2009.6, data.table(character(0), NULL), data.table(V1=character()))
test(2009.7, as.data.table(list(y = character(0), x = NULL)), data.table(y=character()))

# use.names=NA warning for out-of-order; https://github.com/Rdatatable/data.table/pull/3455#issuecomment-472744347
DT1 = data.table(a=1:2, b=5:6)
Expand Down