|
23 | 23 | #' |
24 | 24 | #' @export |
25 | 25 | setGeneric("get_warnings", function(object) { |
26 | | - # this line forces evaluation of object before passing to the generic |
27 | | - # needed for error handling to work properly |
28 | | - grDevices::pdf(nullfile()) |
29 | | - on.exit(grDevices::dev.off()) |
30 | | - object |
31 | | - |
| 26 | + dev_suppress(object) |
32 | 27 | standardGeneric("get_warnings") |
33 | 28 | }) |
34 | 29 |
|
35 | | -setMethod("get_warnings", signature = c("qenv"), function(object) { |
| 30 | +setMethod("get_warnings", signature = "qenv", function(object) { |
36 | 31 | warnings <- lapply(object@code, "attr", "warning") |
37 | | - idx_warn <- which(sapply(warnings, Negate(is.null))) |
38 | | - warnings <- warnings[idx_warn] |
39 | | - code <- object@code[idx_warn] |
40 | | - if (length(warnings) == 0) { |
| 32 | + idx_warn <- which(sapply(warnings, function(x) !is.null(x) && !identical(x, ""))) |
| 33 | + if (!any(idx_warn)) { |
41 | 34 | return(NULL) |
42 | 35 | } |
| 36 | + warnings <- warnings[idx_warn] |
| 37 | + code <- object@code[idx_warn] |
43 | 38 |
|
44 | 39 | lines <- mapply( |
45 | 40 | function(warn, expr) { |
46 | | - if (warn == "") { |
47 | | - return(NULL) |
48 | | - } |
49 | | - sprintf("%swhen running code:\n%s", warn, paste(lang2calls(expr), collapse = "\n")) |
| 41 | + sprintf("%swhen running code:\n%s", warn, expr) |
50 | 42 | }, |
51 | 43 | warn = warnings, |
52 | 44 | expr = code |
53 | 45 | ) |
54 | | - lines <- Filter(Negate(is.null), lines) |
55 | 46 |
|
56 | | - paste0( |
57 | | - sprintf("~~~ Warnings ~~~\n\n%s\n\n", paste(lines, collapse = "\n\n")), |
58 | | - sprintf("~~~ Trace ~~~\n\n%s", paste(get_code(object), collapse = "\n")) |
| 47 | + sprintf( |
| 48 | + "~~~ Warnings ~~~\n\n%s\n\n~~~ Trace ~~~\n\n%s", |
| 49 | + paste(lines, collapse = "\n\n"), |
| 50 | + paste(get_code(object), collapse = "\n") |
59 | 51 | ) |
60 | 52 | }) |
61 | 53 |
|
62 | | -setMethod("get_warnings", signature = c("qenv.error"), function(object) { |
| 54 | +setMethod("get_warnings", signature = "qenv.error", function(object) { |
63 | 55 | NULL |
64 | 56 | }) |
65 | 57 |
|
|
0 commit comments