measure_map_safely() is a fault-tolerant version of measure_map() that
captures errors instead of stopping execution. This is useful when exploring
data that may have problematic samples.
Usage
measure_map_safely(
.data,
.f,
.cols = NULL,
...,
.otherwise = NULL,
.error_call = rlang::caller_env()
)Arguments
- .data
A data frame containing one or more
measure_listcolumns.- .f
A function or formula to apply to each sample's measurement tibble.
If a function, it is used as-is.
If a formula (e.g.,
~ { .x$value <- log(.x$value); .x }), it is converted to a function usingrlang::as_function().
- .cols
<
tidy-select> Columns to apply the transformation to. Defaults to allmeasure_listcolumns.- ...
Additional arguments passed to
.f.- .otherwise
Value to use when
.ffails for a sample. Default isNULL, which keeps the original (untransformed) measurement.- .error_call
The execution environment for error reporting.
Value
A list with two elements:
result: A data frame with transformations applied where successfulerrors: A tibble with columnscolumn,sample, anderror
See also
measure_map() for standard (fail-fast) mapping
Examples
library(recipes)
rec <- recipe(water + fat + protein ~ ., data = meats_long) |>
update_role(id, new_role = "id") |>
step_measure_input_long(transmittance, location = vars(channel)) |>
prep()
baked_data <- bake(rec, new_data = NULL)
# A function that might fail for some samples
risky_transform <- function(x) {
if (any(x$value < 0)) stop("Negative values not allowed")
x$value <- log(x$value)
x
}
# Errors are captured, not thrown
result <- measure_map_safely(baked_data, risky_transform)
# Check which samples failed
if (nrow(result$errors) > 0) {
print(result$errors)
}