Skip to content

Commit 55a6e14

Browse files
fixing key setting order
1 parent 3afc750 commit 55a6e14

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

R/as.data.table.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,21 @@ 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.
218-
if (!identical(class(x), "data.frame")) return(as.data.table(as.data.frame(x)))
217+
if (is.data.table(x)) return(as.data.table.data.table(x,keep.rownames = keep.rownames, 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.
218+
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,
221221
# kludge it to 'rn' since we only apply the new name afterwards, #4468
222222
if (is.character(keep.rownames) && identical(keep.rownames, key)) key='rn'
223-
ans = data.table(rn=rownames(x), x, keep.rownames=FALSE, key=key)
223+
ans = data.table(rn=rownames(x), x, keep.rownames=FALSE)
224224
if (is.character(keep.rownames))
225225
setnames(ans, 'rn', keep.rownames[1L])
226226
return(ans)
227227
}
228228
if (any(cols_with_dims(x))) {
229229
# a data.frame with a column that is data.frame needs to be expanded; test 2013.4
230230
# x may be a class with [[ method that behaves differently, so as.list first for default [[, #4526
231-
return(as.data.table.list(as.list(x), keep.rownames=keep.rownames, ...))
231+
return(as.data.table.list(as.list(x), keep.rownames=keep.rownames, key = key,...))
232232
}
233233
ans = copy(x) # TO DO: change this deep copy to be shallow.
234234
setattr(ans, "row.names", .set_row_names(nrow(x)))
@@ -241,17 +241,22 @@ as.data.table.data.frame = function(x, keep.rownames=FALSE, key=NULL, ...) {
241241
# fix for #1078 and #1128, see .resetclass() for explanation.
242242
setattr(ans, "class", .resetclass(x, "data.frame"))
243243
setalloccol(ans)
244-
setkeyv(ans, key)
244+
if (!is.null(key)) setkeyv(ans, key)
245245
ans
246246
}
247247

248-
as.data.table.data.table = function(x, ...) {
248+
as.data.table.data.table = function(x, keep.rownames = FALSE, key = NULL, ...) {
249249
# as.data.table always returns a copy, automatically takes care of #473
250250
if (any(cols_with_dims(x))) { # for test 2089.2
251-
return(as.data.table.list(x, ...))
251+
return(as.data.table.list(x, keep.rownames = keep.rownames, key = key, ...))
252252
}
253253
x = copy(x) # #1681
254254
# fix for #1078 and #1128, see .resetclass() for explanation.
255255
setattr(x, 'class', .resetclass(x, "data.table"))
256+
if (!is.null(key)) {
257+
setkeyv(x, key)
258+
} else {
259+
setkeyv(x, NULL)
260+
}
256261
x
257262
}

0 commit comments

Comments
 (0)