-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Collect warnings and add dplyr_last_warnings()
#6443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c83a292
d186b65
1db6c14
cd839a2
0f688a6
8f0724c
91102cf
6e63112
3852fd9
f89606f
c6fa184
9f54ab1
b446a7f
1979652
88d0393
83b711b
e9f3f8d
5801551
6738fb0
219a10c
716acc1
4acbaee
104bddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,12 +77,7 @@ quo_as_label <- function(quo) { | |
} | ||
|
||
local_error_context <- function(dots, .index, mask, frame = caller_env()) { | ||
expr <- dots[[.index]] | ||
if (quo_is_call(expr, "invisible")) { | ||
expr <- "" | ||
} else { | ||
expr <- quo_as_label(dots[[.index]]) | ||
} | ||
expr <- dot_as_label(dots[[.index]]) | ||
|
||
error_context <- env( | ||
error_name = arg_name(dots, .index), | ||
|
@@ -95,6 +90,25 @@ peek_error_context <- function() { | |
context_peek("dplyr_error_context", "peek_error_context", "dplyr error handling") | ||
} | ||
|
||
dot_as_label <- function(expr) { | ||
if (quo_is_call(expr, "invisible")) { | ||
"" | ||
} else { | ||
quo_as_label(expr) | ||
} | ||
} | ||
|
||
mask_type <- function(mask = peek_mask()) { | ||
if (mask$get_size() > 0) { | ||
if (mask$is_grouped_df()) { | ||
return("grouped") | ||
} else if (mask$is_rowwise_df()) { | ||
return("rowwise") | ||
} | ||
} | ||
"ungrouped" | ||
} | ||
|
||
cnd_bullet_header <- function(what) { | ||
error_context <- peek_error_context() | ||
error_name <- error_context$error_name | ||
|
@@ -164,3 +178,147 @@ skip_internal_condition <- function(cnd) { | |
cnd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat that this stashes all 4 warnings, but it is a little hard to know which sets of warnings came from which expressions in library(dplyr)
fn <- function() {
throws_warning <- mtcars %>%
group_by(vs) %>%
mutate(mpg = sqrt(-mpg))
throws_warning2 <- mtcars %>%
group_by(vs) %>%
mutate(mpg = sqrt(-mpg))
}
fn() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I don't know how to do better here. If you source the file you'll get srcrefs buried in the backtraces of Each |
||
} | ||
} | ||
|
||
|
||
# Warnings ------------------------------------------------------------- | ||
|
||
#' Show warnings from the last command | ||
#' | ||
#' Warnings that occur inside a dplyr verb like `mutate()` are caught | ||
#' and stashed away instead of being emitted to the console. This | ||
#' prevents rowwise and grouped data frames from flooding the console | ||
#' with warnings. To see the original warnings, use | ||
#' `last_dplyr_warnings()`. | ||
#' | ||
#' @param n Passed to [head()] so that only the first `n` warnings are | ||
#' displayed. | ||
#' @keywords internal | ||
#' @export | ||
last_dplyr_warnings <- function(n = 5) { | ||
if (!identical(n, Inf)) { | ||
check_number(n) | ||
stopifnot(n >= 0) | ||
lionel- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
warnings <- the$last_warnings | ||
n_remaining <- max(length(warnings) - n, 0L) | ||
|
||
warnings <- head(warnings, n = n) | ||
warnings <- map(warnings, new_dplyr_warning) | ||
|
||
structure( | ||
warnings, | ||
class = c("last_dplyr_warnings", "list"), | ||
n_shown = n, | ||
n_remaining = n_remaining | ||
) | ||
} | ||
|
||
on_load({ | ||
the$last_warnings <- list() | ||
the$last_cmd_frame <- "" | ||
}) | ||
|
||
# Flushes warnings if a new top-level command is detected | ||
push_dplyr_warnings <- function(warnings) { | ||
last <- the$last_cmd_frame | ||
current <- obj_address(sys.frame(1)) | ||
|
||
if (!identical(last, current)) { | ||
reset_dplyr_warnings() | ||
the$last_cmd_frame <- current | ||
} | ||
|
||
the$last_warnings <- c(the$last_warnings, warnings) | ||
} | ||
|
||
# Also used in tests | ||
reset_dplyr_warnings <- function() { | ||
the$last_warnings <- list() | ||
} | ||
|
||
signal_warnings <- function(warnings, error_call) { | ||
n <- length(warnings) | ||
if (!n) { | ||
return() | ||
} | ||
|
||
push_dplyr_warnings(warnings) | ||
|
||
first <- new_dplyr_warning(warnings[[1]]) | ||
call <- format_error_call(error_call) | ||
|
||
msg <- paste_line( | ||
cli::format_warning(c( | ||
"There {cli::qty(n)} {?was/were} {n} warning{?s} in a {call} step.", | ||
if (n > 1) "The first warning was:" | ||
)), | ||
paste(cli::col_yellow("!"), cnd_message(first)) | ||
) | ||
|
||
# https://github.com/r-lib/cli/issues/525 | ||
if (n > 1) { | ||
msg <- paste_line( | ||
msg, | ||
cli::format_warning(c( | ||
i = if (n > 1) "Run {.run dplyr::last_dplyr_warnings()} to see the {n - 1} remaining warning{?s}." | ||
)) | ||
) | ||
} | ||
|
||
warn(msg, use_cli_format = FALSE) | ||
} | ||
|
||
new_dplyr_warning <- function(data) { | ||
label <- cur_group_label( | ||
data$type, | ||
data$group_data$id, | ||
data$group_data$group | ||
) | ||
if (nzchar(label)) { | ||
label <- glue(" in {label}") | ||
} | ||
|
||
msg <- glue::glue("Problem{label} while computing `{data$name} = {data$expr}`.") | ||
|
||
warning_cnd( | ||
message = msg, | ||
parent = data$cnd, | ||
call = data$call, | ||
trace = data$cnd$trace | ||
) | ||
} | ||
|
||
#' @export | ||
print.last_dplyr_warnings <- function(x, ...) { | ||
# Opt into experimental grayed out tree | ||
local_options( | ||
"rlang:::trace_display_tree" = TRUE | ||
) | ||
print(unstructure(x), ..., simplify = "none") | ||
|
||
n_remaining <- attr(x, "n_remaining") | ||
if (n_remaining) { | ||
n_more <- attr(x, "n_shown") * 2 | ||
cli::cli_bullets(c( | ||
"... with {n_remaining} more warning{?s}.", | ||
"i" = "Run {.run dplyr::last_dplyr_warnings(n = {n_more})} to show more." | ||
)) | ||
} | ||
} | ||
|
||
# rlang should export this routine | ||
error_call <- function(call) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a hack to get the result of Tracked in r-lib/rlang#1474. |
||
tryCatch( | ||
abort("", call = call), | ||
error = conditionCall | ||
) | ||
} | ||
|
||
cnd_message_lines <- function(cnd, ...) { | ||
c( | ||
"!" = cnd_header(cnd, ...), | ||
cnd_body(cnd, ...), | ||
cnd_footer(cnd, ...) | ||
) | ||
} | ||
lionel- marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,15 +96,44 @@ group_labels_details <- function(keys) { | |
}), ", ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably backtick the |
||
} | ||
|
||
cur_group_label <- function() { | ||
mask <- peek_mask() | ||
if(mask$is_grouped_df() && mask$get_size() > 0) { | ||
glue("group {id}: {label}", id = cur_group_id(), label = group_labels_details(cur_group())) | ||
} else if (mask$is_rowwise_df() && mask$get_size() > 0) { | ||
paste0("row ", cur_group_id()) | ||
} else { | ||
"" | ||
} | ||
cur_group_label <- function(type = mask_type(), | ||
id = cur_group_id(), | ||
group = cur_group()) { | ||
switch( | ||
type, | ||
ungrouped = "", | ||
grouped = glue("group {id}: {label}", label = group_labels_details(group)), | ||
rowwise = glue("row {id}"), | ||
stop_mask_type(type) | ||
) | ||
} | ||
|
||
cur_group_data <- function(mask_type) { | ||
switch( | ||
mask_type, | ||
ungrouped = list(), | ||
grouped = list(id = cur_group_id(), group = cur_group()), | ||
rowwise = list(id = cur_group_id()), | ||
stop_mask_type(mask_type) | ||
) | ||
} | ||
|
||
stop_mask_type <- function(type) { | ||
cli::cli_abort( | ||
"Unexpected mask type {.val {type}}.", | ||
.internal = TRUE | ||
) | ||
} | ||
|
||
cnd_data <- function(cnd, ctxt, mask_type, call) { | ||
list( | ||
cnd = cnd, | ||
name = ctxt$error_name, | ||
expr = ctxt$error_expression, | ||
type = mask_type, | ||
group_data = cur_group_data(mask_type), | ||
call = call | ||
) | ||
} | ||
|
||
#' @rdname context | ||
|
@@ -132,6 +161,7 @@ context_local <- function(name, value, frame = caller_env()) { | |
old <- context_poke(name, value) | ||
expr <- expr(on.exit(context_poke(!!name, !!old), add = TRUE)) | ||
eval_bare(expr, frame) | ||
value | ||
} | ||
|
||
peek_column <- function(call = caller_env()) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.