0

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?

4

1 回答 1

1

编辑

这是一个比我最初提供的更好的例子。您所需要的只是将您的模型包装在一个自定义函数中,该函数将失败转化为NAs。

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"

reprex 包(v0.3.0)于 2019 年 11 月 14 日创建

原始答案

这是可能的,但它需要一些自定义代码。下面,我们需要检查是否x存在NULL

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

reprex 包(v0.3.0)于 2019 年 11 月 14 日创建

于 2019-11-14T14:59:04.400 回答