diff --git a/NAMESPACE b/NAMESPACE index 25dfbd98e..57472a5bf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -44,6 +44,7 @@ S3method(as.data.table, ordered) S3method(as.data.table, Date) S3method(as.data.table, table) S3method(as.data.table, xts) +S3method(as.data.table, default) S3method(as.data.frame, data.table) S3method(as.list, data.table) S3method(as.matrix, data.table) diff --git a/R/data.table.R b/R/data.table.R index e514d5287..041c504e1 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -1632,7 +1632,7 @@ as.matrix.data.table <- function(x,...) X } -as.data.table.matrix <- function(x, keep.rownames=FALSE) +as.data.table.matrix <- function(x, keep.rownames=FALSE, ...) { if (keep.rownames) return(data.table(rn=rownames(x), x, keep.rownames=FALSE)) d <- dim(x) @@ -1661,7 +1661,7 @@ as.data.table.matrix <- function(x, keep.rownames=FALSE) alloc.col(value) } -as.data.table.data.frame <- function(x, keep.rownames=FALSE) +as.data.table.data.frame <- function(x, keep.rownames=FALSE, ...) { if (keep.rownames) return(data.table(rn=rownames(x), x, keep.rownames=FALSE)) ans = copy(x) # TO DO: change this deep copy to be shallow. @@ -1675,7 +1675,7 @@ as.data.table.data.frame <- function(x, keep.rownames=FALSE) alloc.col(ans) } -as.data.table.list <- function(x, keep.rownames=FALSE) { +as.data.table.list <- function(x, keep.rownames=FALSE, ...) { if (!length(x)) return( null.data.table() ) n = vapply(x, length, 0L) mn = max(n) @@ -1697,13 +1697,13 @@ as.data.table.list <- function(x, keep.rownames=FALSE) { alloc.col(x) } -as.data.table.data.table <- function(x, keep.rownames=FALSE) return(x) +as.data.table.data.table <- function(x, keep.rownames=FALSE, ...) return(x) # takes care of logical, character, numeric, integer as.data.table.factor <- as.data.table.ordered <- as.data.table.integer <- as.data.table.numeric <- as.data.table.logical <- as.data.table.character <- -as.data.table.Date <- function(x, keep.rownames=FALSE) { +as.data.table.Date <- function(x, keep.rownames=FALSE, ...) { tt = deparse(substitute(x))[1] nm = names(x) # FR #2356 - transfer names of named vector as "rn" column if required @@ -1734,7 +1734,7 @@ R300_provideDimnames <- function (x, sep = "", base = list(LETTERS)) # backpor } # as.data.table.table - FR #4848 -as.data.table.table <- function(x, keep.rownames=FALSE) { +as.data.table.table <- function(x, keep.rownames=FALSE, ...) { # Fix for bug #5408 - order of columns are different when doing as.data.table(with(DT, table(x, y))) val = rev(dimnames(R300_provideDimnames(x))) if (is.null(names(val)) || all(nchar(names(val)) == 0L)) @@ -1836,13 +1836,17 @@ tail.data.table <- function(x, n=6, ...) { `[<-.data.table`(x,j=name,value=value) # important i is missing here } -as.data.table <-function(x, keep.rownames=FALSE) +as.data.table <-function(x, keep.rownames=FALSE, ...) { if (is.null(x)) return(null.data.table()) UseMethod("as.data.table") } +as.data.table.default <- function(x, ...){ + setDT(as.data.frame(x, ...))[] +} + as.data.frame.data.table <- function(x, ...) { ans = copy(x) diff --git a/R/xts.R b/R/xts.R index 0c07f7e3c..24c90db52 100644 --- a/R/xts.R +++ b/R/xts.R @@ -1,4 +1,4 @@ -as.data.table.xts <- function(x, keep.rownames = TRUE){ +as.data.table.xts <- function(x, keep.rownames = TRUE, ...){ stopifnot(requireNamespace("xts"), !missing(x), xts::is.xts(x)) if(!keep.rownames) return(setDT(as.data.frame(x, row.names=FALSE))[]) if("index" %in% names(x)) stop("Input xts object should not have 'index' column because it would result in duplicate column names. Rename 'index' column in xts or use `keep.rownames=FALSE` and add index manually as another column.") diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1a570e1ef..94fa331db 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -5741,6 +5741,12 @@ if ("package:xts" %in% search()) { test(1465.4, xt, xt_dt) } +# as.data.table.default #969 +ar <- array(NA, dim=c(10,4),dimnames = list(NULL,paste0("col",1:4))) +test(1466.1, as.data.table(as.data.frame(ar)), as.data.table(ar)) # array type +x <- rep(Sys.time(),3) +test(1466.2, as.data.table(as.data.frame(x)), as.data.table(x)) # posix type + ########################## diff --git a/man/as.data.table.xts.Rd b/man/as.data.table.xts.Rd index 52f0746ab..a38fe2b7f 100644 --- a/man/as.data.table.xts.Rd +++ b/man/as.data.table.xts.Rd @@ -5,12 +5,14 @@ Efficient conversion xts to data.table. } \usage{ -\method{as.data.table}{xts}(x, keep.rownames = TRUE) +\method{as.data.table}{xts}(x, keep.rownames = TRUE, ...) } \arguments{ \item{x}{xts to convert to data.table} \item{keep.rownames}{keep xts index as \emph{index} column in result data.table} + +\item{\dots}{ignored, just for consistency with \code{as.data.table}} } \seealso{ \code{\link{as.xts.data.table}} } \examples{