Skip to content

Commit 27bc253

Browse files
corrected last test
1 parent f404b02 commit 27bc253

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

R/as.data.table.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ as.data.table.list = function(x,
214214
}
215215

216216
as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
217-
if (is.data.table(x)) return(as.data.table.data.table(x)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
217+
if (is.data.table(x)) return(as.data.table.data.table(x, key=key)) # S3 is weird, #6739. Also # nocov; this is tested in 2302.{2,3}, not sure why it doesn't show up in coverage.
218218
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x), keep.rownames=keep.rownames, key=key, ...))
219219
if (!isFALSE(keep.rownames)) {
220220
# can specify col name to keep.rownames, #575; if it's the same as key,
@@ -245,17 +245,19 @@ as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
245245
ans
246246
}
247247

248-
as.data.table.data.table = function(x, keep.rownames = FALSE, ...) {
248+
as.data.table.data.table = function(x, ...) {
249+
key = list(...)$key
249250
# as.data.table always returns a copy, automatically takes care of #473
250251
if (any(cols_with_dims(x))) { # for test 2089.2
251-
return(as.data.table.list(x,keep.rownames = keep.rownames, ...))
252+
return(as.data.table.list(x, ...))
252253
}
253254
x = copy(x) # #1681
254255
# fix for #1078 and #1128, see .resetclass() for explanation.
255256
setattr(x, 'class', .resetclass(x, "data.table"))
256-
if (methods::hasArg("key")) {
257-
key <- list(...)$key
257+
if (!is.null(key)) {
258258
setkeyv(x, key)
259+
} else {
260+
setkey(x, NULL)
259261
}
260262
x
261263
}

inst/tests/tests.Rraw

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21086,7 +21086,18 @@ test(2308.01, fread("date\nNA\n2014-12-05", keepLeadingZeros=TRUE), dt)
2108621086
test(2308.02, fread("date\nNA\n2014-12-05", keepLeadingZeros=FALSE), dt)
2108721087

2108821088
# Test that as.data.table.data.table preserves key when explicitly specified but not when omitted
21089-
x <- data.frame(t = c(3:1,4:5), y = 1:5)
21090-
class(x) <- c("tbl_df", "tbl", "data.frame")
21091-
test(2309.01, {key(as.data.table(x, key = "t"))}, "t")
21092-
test(2309.02, key(as.data.table(data.table(t = c(3:1,4:5), key = "t"))), "t")
21089+
# data.frame to data.table with key
21090+
DT <- as.data.table(data.frame(t = c(3:1, 4:5), y = 1:5))
21091+
setkey(DT, t)
21092+
test(2309.01, key(DT), "t")
21093+
# tibble to data.table with key
21094+
DT <- as.data.table(tibble(t = c(3:1, 4:5), y = 1:5))
21095+
setkey(DT, t)
21096+
test(2309.02, key(DT), "t")
21097+
# data.table to data.table with key
21098+
DT <- as.data.table(data.table(t = c(3:1, 4:5), y = 1:5))
21099+
setkey(DT, t)
21100+
test(2309.03, key(DT), "t")
21101+
# data.table to data.table without key
21102+
DT <- as.data.table(.table(t = c(3:1, 4:5), key = "t"))
21103+
test(2309.04, key(DT), NULL)

man/as.data.table.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Functions to check if an object is \code{data.table}, or coerce it if possible.
2121
\usage{
2222
as.data.table(x, keep.rownames=FALSE, \dots)
2323

24-
\method{as.data.table}{data.table}(x, keep.rownames = FALSE, \dots)
24+
\method{as.data.table}{data.table}(x, \dots)
2525

2626
\method{as.data.table}{array}(x, keep.rownames=FALSE, key=NULL, sorted=TRUE,
2727
value.name="value", na.rm=TRUE, \dots)

0 commit comments

Comments
 (0)