Reputation: 195
I am fitting a bunch of models using a drake plan. Some of them are failing due to problems in the initialization. I am running `make(plan, keep_going = T) in order to finish the plan in anyway, but what I would really like is to be able to skip the failed targets and treat them as missing values in the rest of the plan.
Is there anyway to replace failed targets with, let's say a constant NA
symbol?
Upvotes: 0
Views: 47
Reputation: 5841
Here is a better example than the one I originally provided. All you need is to wrap your models in a custom function that turns failures into NA
s.
library(drake)
fail_na <- function(code) {
tryCatch(code, error = error_na)
}
error_na <- function(e) {
NA
}
plan <- drake_plan(
failure = fail_na(stop()),
success = fail_na("success")
)
make(plan, keep_going = TRUE)
#> In drake, consider r_make() instead of make(). r_make() runs make() in a fresh R session for enhanced robustness and reproducibility.
#> target success
#> target failure
readd(failure)
#> [1] NA
readd(success)
#> [1] "success"
Created on 2019-11-14 by the reprex package (v0.3.0)
It is possible, but it requires some custom code. Below, we need to check that x
is NULL
or missing.
library(drake)
`%||%` <- function(x, y) {
if (is.null(x)) {
y
} else {
x
}
}
na_fallback <- function(x) {
out <- tryCatch(
x %||% NA,
error = function(e) {
NA
}
)
out
}
plan <- drake_plan(
x = stop(),
y = na_fallback(x)
)
make(plan, keep_going = TRUE)
#> In drake, consider r_make() instead of make(). r_make() runs make() in a fresh R session for enhanced robustness and reproducibility.
#> target x
#> fail x
#> target y
readd(y)
#> [1] NA
Created on 2019-11-14 by the reprex package (v0.3.0)
Upvotes: 1