Matthew Kuperus Heun
Matthew Kuperus Heun

Reputation: 123

How to create a plan in a function

I'd like to use a function to create a drake plan. See MWE:

plan_func <- function(param) {
  drake::drake_plan(
    myparam = param
  )
}

I would like

plan_func("a")

to give

# A tibble: 1 x 2
  target  command   
  <chr>   <expr_lst>
1 myparam "a"

but instead, it gives

> plan_func("a")
# A tibble: 1 x 2
  target  command   
  <chr>   <expr_lst>
1 myparam param     

It feels like this is an NSE problem. Can someone give a friendly hint how to get this right?

My appreciation in advance!

Upvotes: 0

Views: 60

Answers (1)

landau
landau

Reputation: 5841

drake_plan() supports tidy evaluation, so you can write !!param in the plan.

library(drake)
plan_func <- function(param) {
  drake::drake_plan(
    myparam = !!param
  )
}
plan_func("a")
#> # A tibble: 1 x 2
#>   target  command   
#>   <chr>   <expr_lst>
#> 1 myparam "a"

Created on 2020-06-02 by the reprex package (v0.3.0)

Trickier situations like https://github.com/ropensci/drake/issues/1251 might require you to turn off the tidy_eval and transform arguments of drake_plan().

Edit

To splice multiple arguments into a function, use triple-bang (!!!) instead of bang-bang (!!):

library(drake)

plan_func <- function(param) {
  drake_plan(
    myparam = f(!!param)
  )
}

  plan_func(c("a", "b"))
#> # A tibble: 1 x 2
#>   target  command       
#>   <chr>   <expr>        
#> 1 myparam f(c("a", "b"))

plan_func <- function(param) {
  drake_plan(
    myparam = f(!!!param)
  )
}

plan_func(c("a", "b"))
#> # A tibble: 1 x 2
#>   target  command    
#>   <chr>   <expr>     
#> 1 myparam f("a", "b")

Created on 2020-06-02 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions