Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can we use cancel_if() with dynamic branching? #1218

Closed
3 tasks done
ginolhac opened this issue Mar 13, 2020 · 2 comments
Closed
3 tasks done

can we use cancel_if() with dynamic branching? #1218

ginolhac opened this issue Mar 13, 2020 · 2 comments

Comments

@ginolhac
Copy link

Prework

  • Read and abide by drake's code of conduct.
  • Search for duplicates among the existing issues, both open and closed.
  • If you think your question has a quick and definite answer, consider posting to Stack Overflow under the drake-r-package tag. (If you anticipate extended follow-up and discussion, you are already in the right place!)

Question

Hello,
your work is very much impressive and I'd to thank you for the hard work and dedication to this great package. I have a first success building a Rmd website with renv and gitlab-CI, super nice. The examples shared really helped a lot. Of course, now I want more complex things and I am wondering how to cancel a target with dynamic branching.
I know you are working getting sub-targets working for dynamic and it sounds complex (#1214), here I want to cancel a complete dynamic target based on a condition. I tried the
trigger = trigger(condition())

but got:

Error: Dynamic grouping variables are forbidden in the condition and change triggers. Found dynamic grouping variables for target downstream:
  empty

Then, I went to #1131 and thought this could be nice (thanks for #1212 BTW, that I discover testing the dev version of drake) with cancel_if()

library(drake)

f <- function(x) {
  cancel_if(length(x) < 1)
  "x"
}

plan <- drake_plan(
  empty = numeric(0), # with numeric(1) works
  downstream = target(f(empty), 
                     # trigger = trigger(condition = length(empty) > 0),
                      dynamic = map(empty))
)

make(plan)
#> ▶ target empty
#> ▶ dynamic downstream
#> > subtarget downstream_
#> Error: dynamic grouping variable empty needs more than 0 elements.

Created on 2020-03-13 by the reprex package (v0.3.0)

so my question is:
how can I stop a dynamic target from being done based on a condition like no elements? In my case, I am building a teaching website, I have lecture / practicals / projects and they are all dynamic since appearing as the course goes on. With at least one of each, everything is fine.
I can also use keep_going = TRUE to survive one target failure but I don't like it. I'd prefer to invalidate right away a target (and dependencies) if basically nothing needs to be done, like no Rmd in practicals for example?
Many thanks in advance,

@wlandau
Copy link
Member

wlandau commented Mar 14, 2020

Thanks for the support and encouragement. The best workaround I can think of is to flag emptiness with an NA and then cancel when you see it.

library(drake)

f <- function(x) {
  cancel_if(is.na(x)) # Cancel if you see the padded NA
  "x"
}

na_empty <- function(x) {
  if (!length(x)) {
    x <- NA
  }
  x
}

# Empty case
plan <- drake_plan(
  empty = na_empty(numeric(0)),
  downstream = target(
    f(empty), 
    dynamic = map(empty)
  )
)

make(plan)
#> ▶ target empty
#> ▶ dynamic downstream
#> > subtarget downstream_e6fd16da
#> ■ cancel downstream_e6fd16da
#> ■ finalize downstream

# Nonempty case
plan <- drake_plan(
  empty = na_empty(numeric(1)),
  downstream = target(
    f(empty), 
    dynamic = map(empty)
  )
)

make(plan)
#> ▶ target empty
#> ▶ dynamic downstream
#> > subtarget downstream_b3ddad2c
#> ■ finalize downstream

Created on 2020-03-13 by the reprex package (v0.3.0)

@wlandau wlandau closed this as completed Mar 14, 2020
@ginolhac
Copy link
Author

Wow! thanks a lot, that's really nice, and easy enough.
I can safely drop the keep_going = TRUE and even the cancel gets its own orange color ;)
thanks again for such a fast help

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants