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

Use the .by argument of combine in command #1340

Closed
5 of 6 tasks
vkehayas opened this issue Nov 3, 2020 · 1 comment
Closed
5 of 6 tasks

Use the .by argument of combine in command #1340

vkehayas opened this issue Nov 3, 2020 · 1 comment

Comments

@vkehayas
Copy link
Contributor

vkehayas commented Nov 3, 2020

Prework

  • Read and agree to the code of conduct and contributing guidelines.
  • If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
  • For any problems you identify, a minimal reproducible example so the maintainer can troubleshoot. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

Question

I would like to use a grouping variable inside the command that builds a target.
Using drake's combine returns an object that cannot be used directly.

Reproducible example

library(drake)
inputFrame = expand.grid(id = c("a", "b"),
                        n1 = c(10, 100))

spamPlan = drake_plan(
  x = target(runif(n1),
             transform = map(n1,
                             .data = !!inputFrame)),
  y = target(x*100,
              transform = map(x)),
  z = target(data.frame(s = sum(y),
                        id = id), # This is the crucial step that fails
             transform = combine(y,
                                 id,
                                 .by = id))
)

> spamPlan
# A tibble: 10 x 2
   target    command                                             
   <chr>     <expr>                                              
 1 x_a_10    runif(10)                                           
 2 x_b_10    runif(10)                                           
 3 x_a_100   runif(100)                                          
 4 x_b_100   runif(100)                                          
 5 y_x_a_10  x_a_10 * 100                                        
 6 y_x_b_10  x_b_10 * 100                                        
 7 y_x_a_100 x_a_100 * 100                                       
 8 y_x_b_100 x_b_100 * 100                                       
 9 z_a       data.frame(s = sum(y_x_a_10, y_x_a_100), id = `"a"`)
10 z_b       data.frame(s = sum(y_x_b_10, y_x_b_100), id = `"b"`)

> make(spamPlan)
▶ target x_b_10target x_a_10target x_b_100target x_a_100target y_x_b_10target y_x_a_10target y_x_b_100target y_x_a_100target z_b
x fail z_b
Error: target z_b failed.
diagnose(z_b)error$message:
  object '"b"' not found
diagnose(z_b)error$calls:
  1. └─base::data.frame(s = sum(y_x_b_10, y_x_b_100), id = `"b"`)

I am not sure why this happens but the problem is solved if I use paste(quote(id)) like in this plan:

spamPlan = drake_plan(
  x = target(runif(n1),
             transform = map(n1,
                             .data = !!inputFrame)),
  y = target(x*100,
              transform = map(x)),
  z = target(data.frame(s = sum(y),
                        id = as.character(quote(id))), 
             transform = combine(y,
                                 id,
                                 .by = id))
)

Is it worth considering to perform such a transformation to the appropriate class within drake?
The variable id is part of the plan and I would expect that the variable should be resolved.

@wlandau
Copy link
Member

wlandau commented Nov 3, 2020

Thanks, should be fixed now.

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