Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions R/rowwiseDT.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
x = substitute(list(...))[-1L]
if (is.null(nms <- names(x)))
stopf("Must provide at least one column (use `name=`). See ?rowwiseDT for details")
if (length(key_pos <- which(nms == "key" & nzchar(x))) > 0L) {
if (length(key_pos) > 1L)
stopf("key specified more than once")
if (length(key <- eval(x[[key_pos]], parent.frame())) == 1L)

Check warning on line 8 in R/rowwiseDT.R

View workflow job for this annotation

GitHub Actions / lint-r

file=R/rowwiseDT.R,line=8,col=65,[trailing_whitespace_linter] Remove trailing whitespace.
Comment thread
MichaelChirico marked this conversation as resolved.
Resolved
key = cols_from_csv(key)
x = x[-key_pos]
nms = nms[-key_pos]
} else {
key_pos = 0L
}
header_pos = which(nzchar(nms))
if (any(nzchar(x[header_pos])))
stopf("Named arguments must be empty")
if (!identical(header_pos, seq_along(header_pos)))
stopf("Header must be the first N arguments")
if (key_pos && !(key_pos - 1L) %in% c(0L, length(header_pos), length(x)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why length(header_pos)? do we lose a lot by making this an error as well?

(it can be a bit hard to map the test case to how it might look in "real" code, so I might be missing something, although this was something mentioned by me as 'bad' in #7905)

test(2282.11, rowwiseDT(A=, B=, key="B", 1, 2), DT)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is because I imagined data.table(a=.., b=.., key = "a") plus a data block, but I agree the extra flexibility is not very worthy here. I'll remove the middle option.

stopf("key specification not allowed in between names or data, only boundaries")
header = nms[header_pos]
ncols = length(header)
body = lapply(x[-header_pos], eval, envir = parent.frame())
Expand All @@ -27,5 +39,7 @@
body = split(body, rep(seq_len(nrows), each = ncols))
ans = rbindlist(body)
setnames(ans, header)
if (key_pos)

Check warning on line 42 in R/rowwiseDT.R

View workflow job for this annotation

GitHub Actions / lint-r

file=R/rowwiseDT.R,line=42,col=15,[trailing_whitespace_linter] Remove trailing whitespace.
Comment thread
MichaelChirico marked this conversation as resolved.
Resolved
setkeyv(ans, key)
ans
}
11 changes: 11 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18843,6 +18843,17 @@ test(2282.08, rowwiseDT(A=,B=,1,2,C=,4), error="Header must be the first N argum
# evaluate arguments in the correct frame
ncols = 1e6
test(2282.09, rowwiseDT(A=,ncols), data.table(A=ncols))
# tests involving key specification in rowwiseDT
test(2282.10, rowwiseDT(key="B",A=,B=,1,2), data.table(A=1,B=2,key="B"))
test(2282.11, rowwiseDT(A=,B=,key="B",1,2), data.table(A=1,B=2,key="B"))
test(2282.12, rowwiseDT(A=,B=,1,2,key="B"), data.table(A=1,B=2,key="B"))
test(2282.13, rowwiseDT(A=,key="B",B=,1,2), error = "key specification not allowed in between names or data, only boundaries")
test(2282.14, rowwiseDT(A=,B=,1,key="B",2), error = "key specification not allowed in between names or data, only boundaries")
test(2282.15, rowwiseDT(A=,key=,B=,1,2,3,key="B"), data.table(A=1,B=3,key="B")[,key:=2][,.(A,key,B)])
test(2282.16, rowwiseDT(key="A",key="B",A=,B=,key=,1,2,3), error = "key specified more than once")
test(2282.17, rowwiseDT(A=,B=,key="A,B",1,2), data.table(A=1,B=2, key="A,B"))
# as "" equals to "missing", key = "" means column name (backwards compatible)
test(2282.18, rowwiseDT(A="",B="",key="",1,2,3), data.table(A=1,B=2)[,key:=3])

# test 2283 moved tests to optimize.Rraw

Expand Down
Loading