| minex | R Documentation |
Reduces a failing piece of R code to the smallest subset of its top-level statements that still triggers the same failure. The result is a one-minimal example: removing any remaining statement makes the failure disappear. This is the form requested when reporting bugs or asking for help, and the part of preparing such an example that is usually done by hand.
minex(
file = NULL,
code = NULL,
clipboard = FALSE,
oracle = NULL,
condition = c("error", "warning", "message", "any"),
match = c("message", "class", "both"),
algorithm = c("cdd", "ddmin"),
backend = c("callr", "inprocess"),
timeout = 60,
max_oracle_calls = Inf,
verbose = FALSE,
granularity = c("statement", "expression")
)
file |
Path to a file containing the R code to minimize. Used only when
neither |
code |
A character vector of R source lines, or a single string. Takes
precedence over both |
clipboard |
Logical. If |
oracle |
Optional predicate taking a character vector of statements and
returning a single logical. When supplied, the target failure is not
recorded automatically and |
condition |
Which kind of condition to target: |
match |
How a candidate's failure must match the recorded one when no
|
algorithm |
The reduction strategy passed to |
backend |
Either |
timeout |
Maximum seconds allowed for a single |
max_oracle_calls |
Numeric upper bound on the number of oracle
evaluations in the reduction search, passed to |
verbose |
Logical. If |
granularity |
When |
When no subset of the top-level statements can be removed – the usual case
when the failure is nested inside a function body – minex() continues
within the surviving statements instead of returning the input unchanged,
so the reduced code is then a simplification of the original statements
rather than a subset of them. See granularity.
By default minex() first runs the whole input to record the failure it
produces (its condition message and class), then uses ddmin() to search for
a minimal subset that reproduces it. Each candidate is evaluated in a separate
R process so that dependencies between statements and their side effects are
respected; removing a statement that a later one needs typically changes the
error, and such a removal is therefore rejected.
Supply a custom oracle to minimize against any condition you can express as
a predicate, rather than against the recorded failure. The oracle receives a
character vector of statements and must return a single logical.
An object of class "minex_result": a list with the minimized code
(a character vector of statements), the original statements, the statement
counts n_original and n_minimal, the character counts n_chars_original
and n_chars_minimal (nchar() of the code collapsed to a single string,
before and after reduction), the number of oracle_calls (every predicate
evaluation, including the failure-point truncation probe), the recorded
target failure (or NULL for a custom oracle), the granularity setting
used, and the match and backend settings.
When the statement pass escalated to "expression", the result also carries
escalated_from = "statement" and coarse_oracle_calls, the share of
oracle_calls spent on the discarded statement-level pass. Both are absent
otherwise, so is.null(res$escalated_from) distinguishes a result that was
reduced at the granularity asked for from one that had to descend.
ddmin() for the underlying algorithm and reduce_rows() for
reducing data frames.
# A failing script padded with irrelevant setup.
script <- c(
"a <- 10",
"b <- 20",
"log('not a number')"
)
res <- minex(code = script, backend = "inprocess")
res
cat(as.character(res), "\n")
# A failure that genuinely depends on an earlier statement: both are kept.
script2 <- c(
"x <- c(1, 2, NA)",
"m <- mean(x)",
"if (is.na(m)) stop('mean is NA')"
)
minex(code = script2, backend = "inprocess")
# The default backend runs candidates in fresh R processes.
minex(code = script)
## Not run:
# Reduce a failing script copied to the system clipboard:
minex(clipboard = TRUE)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.