Skip to content

Commit 74dded2

Browse files
committed
add test for as.data.table.list conversion
1 parent dd80725 commit 74dded2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

R/as.data.table.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ as.data.table.list = function(x,
130130
# e.g. cbind(foo=DF1, bar=DF2) have .named=c(TRUE,TRUE) due to the foo= and bar= and trigger "prefix." for non-vector items
131131
...)
132132
{
133-
if (is.data.table(x)) {
134-
# operate on plain list to avoid [[<-.data.table dispatch
135-
x = as.list(x)
136-
}
137133
n = length(x)
138134
eachnrow = integer(n) # vector of lengths of each column. may not be equal if silent repetition is required.
139135
eachncol = integer(n)
@@ -145,6 +141,12 @@ as.data.table.list = function(x,
145141
rownames_ = NULL
146142
check_rownames = !isFALSE(keep.rownames)
147143

144+
# conversion must happen after capturing names
145+
if (is.data.table(x)) {
146+
# operate on plain list to avoid [[<-.data.table dispatch
147+
x = as.list(x)
148+
}
149+
148150
for (i in seq_len(n)) {
149151
xi = x[[i]]
150152
if (is.null(xi)) next # eachncol already initialized to 0 by integer() above

inst/tests/tests.Rraw

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,8 @@ test(1140, as.data.table(X), data.table(a=c(1:2,1L), b=c(1:3)),
40944094
test(1141.1, data.table(a=1:2, b=1:3), data.table(a=c(1L,2L,1L), b=1:3), warning="Item 1 has 2 rows but longest item has 3; recycled")
40954095
test(1141.2, data.table(a=1:2, data.table(x=1:5, y=6:10)), data.table(a=c(1L,2L,1L,2L,1L), x=1:5, y=6:10), warning="Item 1 has 2 rows but longest item has 5; recycled")
40964096
test(1141.3, data.table(a=1:5, data.table(x=c(1,2), y=c(3,4))), data.table(a=c(1:5), x=c(1,2,1,2,1), y=c(3,4,3,4,3)), warning="Item 2 has 2 rows but longest item has 5; recycled")
4097+
x = structure(list(1, 2), class = c("list", "data.table"))
4098+
test(1141.4, as.data.table(x), data.table(V1=c(1), V2=c(2)))
40974099

40984100
# Fix for bug #79 - DT[, foo()] returns function definition.
40994101
DT <- data.table(a=1:2)

0 commit comments

Comments
 (0)