MUCollective / multiverse

R package for creating explorable multiverse analysis
https://mucollective.github.io/multiverse/
GNU General Public License v3.0
62 stars 5 forks source link

Support conditions #10

Closed mjskay closed 5 years ago

abhsarma commented 5 years ago

I have created basic condition support using %when% and the branch_assert() I have doubts regarding the function name, and the operator used.

mjskay commented 5 years ago

awesome!

Might be worth creating an issue for "figuring out what the branch API should look like" or something like that. One thing I wonder about is this style:

         mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             "sum" ~ (x + y) %when% (values_y == TRUE)
         ))

Versus this style:

         mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             "sum" %when% (values_y == TRUE) ~ x + y
         ))

The second one feels more intuitive to me (the option being chosen is what the "when" is about, not what expression it gets turned into). I'm curious if you feel the other is more intuitive?

mjskay commented 5 years ago

Or it might be crazy, but you could override if and do this:

         mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             if (values_y == TRUE) "sum" ~ x + y
         ))
abhsarma commented 5 years ago
         mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             "sum" %when% (values_y == TRUE) ~ x + y
         ))

Something like this shouldn't be too difficult to implement from where we are at right now. I am recursively looking at the rhs of the ~ for the %when% but can do that on both sides of ~ I guess.

mjskay commented 5 years ago

do you need to look for it recursively? I would guess that it only really is valid if it is used as the top-level expression right?

Like, this is not valid:

         mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             "sum" ~ (x %when% (values_y == TRUE)) + y
         ))

Or am I confused?

abhsarma commented 5 years ago
mutate( z = branch(values_z,
             "constant" ~ 5,
             "linear" ~ x + 1,
             "sum" ~ ((x + y) %when% (values_y == TRUE))
         ))

If someone uses excessive parenthesis, it was giving an error. I just need to find the ast where the top-level call is %when%.

mjskay commented 5 years ago

Oh makes sense! So you unwrap however many parens until you get to non-( call (and maybe braces too?) until the first expression that isn't a ( or { and check for %when%? Anything deeper than that should be an error

abhsarma commented 5 years ago

I can throw the error. But it doesn't right now.

mjskay commented 5 years ago

it probably should throw and error? I don't think it makes sense to let people put it absolutely anywhere in the expression, because the semantics don't really line up with what it looks like: it looks like you're making a subexpression conditional but you're actually making the entire thing conditional.

abhsarma commented 5 years ago

Makes sense. Ok there's a bug: Right now, it just looks past it if it encounters something like this: x %when% (values_y == TRUE)) + y

But not for something like this: x + (y %when% (values_y == TRUE))

Might as well throw the error

mjskay commented 5 years ago

Yeah if you only allow it if %when% is at the top of the tree after unwrapping any sequence of ( or { you should be able to detect both of those

mjskay commented 5 years ago

PS as you collect all these corner cases you should be turning them into tests :-P