I have a list of inputs over which I loop using purrr::map. Using a tweaked version of purrr::possibly (for more details see here) Applying this version of "possibly" to my main function, I'm able to use purrr::map to loop over my input list and store the results, or, alternatively, the complete error or warning messages if the function (partially) fails.
While this works fine for inputs where the function works correctly and inputs where it fails so that an error is produced, it is less optimal when a warning is produced. The resulting output from purrr::map will store only the warning, but not the result.
input <- list(10, -1 , "A")
possibly2 <- function (.f) {
.f <- purrr::as_mapper(.f)
function(...) {
tryCatch(.f(...),
error = function(e) {
return(paste0("Error: ", e$message))
}, warning = function(w) {
return(paste0("Warning: ", w$message))
}
)
}
}
safer_log <- possibly2(.f = log)
map(input, safer_log) %>%
set_names(c("1. good input", "2. warning", "3. error")) # just added for naming the list elements
This results in:
$`1. good input`
[1] 2.302585
$`2. warning`
[1] "Warning: NaNs produced"
$`3. error`
[1] "Error: non-numeric argument to mathematical function"
However, for the warnings, I would actually like to include the results as well. Similar to:
> log(-1)
[1] NaN
Warning message:
In log(-1) : NaNs produced
But I'm not really sure where the result of the main function is stored and how I could adapt the code to return it together with the warning. Probably, in cases where this happens, it is also necessary to split the output into elements $result $warning.