Skip to content

Commit aaa09aa

Browse files
committed
added check
1 parent 053d905 commit aaa09aa

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

R/rowwiseDT.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# This function assumes 'stopf' is available, as it is inside the data.table package.
2+
# If running standalone, replace `stopf(...)` with `stop(sprintf(...))`.
3+
14
rowwiseDT = function(...) {
25
x = substitute(list(...))[-1L]
36
if (is.null(nms <- names(x)))
@@ -13,6 +16,24 @@ rowwiseDT = function(...) {
1316
nrows = length(body) %/% ncols
1417
if (length(body) != nrows * ncols)
1518
stopf("There are %d columns but the number of cells is %d, which is not an integer multiple of the columns", ncols, length(body))
19+
20+
is_problematic = vapply(
21+
body,
22+
function(v) !is.atomic(v) && !is.null(v) && typeof(v) != "list",
23+
FUN.VALUE = logical(1L)
24+
)
25+
26+
if (any(is_problematic)) {
27+
first_problem_idx = which(is_problematic)[1L]
28+
col_idx = (first_problem_idx - 1L) %% ncols + 1L
29+
col_name = header[col_idx]
30+
obj_type = typeof(body[[first_problem_idx]])
31+
stopf(
32+
"In column '%s', received an object of type '%s'.\nComplex objects (like functions, models, etc.) must be wrapped in list() to be stored in a data.table column.\nPlease use `list(...)` for this value.",
33+
col_name,
34+
obj_type
35+
)
36+
}
1637
# make all the non-scalar elements to a list
1738
needs_list = lengths(body) != 1L
1839
body[needs_list] = lapply(body[needs_list], list)

inst/tests/tests.Rraw

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21620,3 +21620,15 @@ local({
2162021620
test(2338.9, {fwrite(dd, f, forceDecimal=FALSE); fread(f)}, di)
2162121621
})
2162221622

21623+
# rowwiseDT() valid and invalid handling of complex objects #7219
21624+
test(2339.1, rowwiseDT(x =, y =, 1, 2, 3, 4), data.table(x = c(1, 3), y = c(2, 4)))
21625+
test(2339.2, rowwiseDT(x =, func =,
21626+
1, list(\(x) x + 1),
21627+
2, list(function(z) z * 2)),
21628+
data.table(x = c(1, 2), func = list(\(x) x + 1, function(z) z * 2)))
21629+
test(2339.3, rowwiseDT(x =, func =, 1, \(x) x + 1),
21630+
error = "In column 'func', received an object of type 'closure'.*wrap.*list")
21631+
test(2339.4, rowwiseDT(x =, expr =, 1, quote(a + b)),
21632+
error = "In column 'expr', received an object of type 'language'.*wrap.*list")
21633+
test(2339.5, rowwiseDT(x =, plist =, 1, as.pairlist(list(123))),
21634+
error = "In column 'plist', received an object of type 'pairlist'.*wrap.*list")

0 commit comments

Comments
 (0)