Skip to content

Commit c132e9a

Browse files
authored
fix(encapsulate): address data table bug (#144)
1 parent 5400fe1 commit c132e9a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

R/encapsulate.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,14 @@ encapsulate = function(method, .f, .args = list(), .opts = list(), .pkgs = chara
196196
}
197197
if (nrow(log)) {
198198
# classes for messages are not really useful, so we save some memory
199-
log[, condition := map(condition, function(x) {
199+
# data.table list columns with 1 row are no fun :(
200+
conds = map(log$condition, function(x) {
200201
if (inherits(x, "message")) {
201202
return(trimws(conditionMessage(x)))
202203
}
203204
x
204-
})]
205+
})
206+
log$condition = if (length(conds) == 1L) list(conds) else conds
205207
}
206208

207209
log$class = factor(log$class, levels = c("output", "warning", "error"), ordered = TRUE)

man/mlr_callbacks.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_encapsulate.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,18 @@ test_that("condition objects are stored", {
227227
expect_equal(res$log$condition[[2]], simpleWarning("b"))
228228
expect_equal(res$log$condition[[3]], simpleError("c"))
229229
}
230+
231+
# data.table with 1 row
232+
fun = function() {
233+
mlr3misc::stopf("c")
234+
}
235+
236+
for (method in c("evaluate", "callr", "mirai", "try")) {
237+
if (!requireNamespace(method, quietly = TRUE)) {
238+
next
239+
}
240+
res = encapsulate(method, fun)
241+
expect_equal(as.character(res$log$class), "error")
242+
expect_equal(res$log$condition[[1]], tryCatch(stopf("c"), error = identity))
243+
}
230244
})

0 commit comments

Comments
 (0)