Comments (8)
Without thinking implementation yet, I am thinking something like the following could be an idea, and can be kept simple in implementation...
mtcars %>% Or(nrow, when = rowcount)
more generally,
x %>% Or(fun, when)
where fun
is a function reference or a lambda, and when
is a (single-valued) condition.
perhaps something like
Or <- function(value, fun, when)
{
if (when) fun(value) else value
}
from magrittr.
my comment above... wrong login...
from magrittr.
This sounds reasonable... I think that, as a name "Or" is too much like a logical OR. Maybe something like fif
would be appropriate.
A couple more thoughts:
- I like the idea of allowing an else function.
- It makes more sense to me to have the condition first, before the function(s) to run.
fif <- function(x, cond, if_fun, else_fun = identity) {
if(cond) if_fun(x)
else else_fun(x)
}
# Some examples
mtcars %>% fif(TRUE, nrow)
mtcars %>% fif(FALSE, nrow)
mtcars %>% fif(TRUE, nrow, head)
mtcars %>% fif(FALSE, nrow, head)
from magrittr.
Right, "Or" is probably not the best -- I quite like when one can "read" the statements.
Another option could be
x %>% when(condition, otherwise = fun)
which can also be "read". Could also have the additional (optional/identity) argument
x %>% when(condition, do = fun1, otherwise = fun2)
although the way this is read is a little different than the first "sentence".
Just a thought.
You're suggestion is fine, although a little insider knowledge is needed to figure out what fif
means...
from magrittr.
That makes sense.
I also realized that you'd probably want to capture the unevaluated function call, so that you could pass in additional arguments. Using your suggestion of function, it would be something like
x %>% when(cond, fun1(opt1, opt2))
So that when cond is TRUE, it would call fun1(x, opt1, opt2)
.
from magrittr.
Right, and we should consider whether the condition should be allowed to depend on x
, e.g.
x %>% when(is.na %>% any, do = fun1(opt1, opt2),
otherwise = fun2(opt3, opt4))
I am thinking we could implement all this using the defer
function we discussed previously.
Something like (this won't work directly, just to show the idea.)
when <- function(x, condition, do = identity, otherwise = NULL)
{
cond <- defer(condition)(x)
if (cond) defer(do)(x) else defer(otherwise)(x)
}
This would be nice if condition depends on x
but more annoying when it doesn't.
Maybe one could think of a syntax to distinguish.
from magrittr.
This is also my number one wanted feature. How about some syntax like this:
input %>%
branch(is.data.fame() %>%
as.matrix() %>%
ensure_symmetric() ) %>%
graph.adjacency()
branch
takes a bunch of chains, the first element of the chain is a logical expression. If all chains inside of branch
are false, then nothing happens to the data, it skips the branch entirely.
Here is another example with a three-way branch:
input %>%
branch(is.data.frame() %>% graph.data.frame(),
is.matrix() %>% graph.adjacency(),
) %>%
plot()
EDIT:
- inspired by metalsmith-branch: https://github.com/ericgj/metalsmith-branch
- of course it would be possible to have nested branches
from magrittr.
This at all interesting anymore, as you can do
mtcars %>% {if(rowcount) nrow(.) else .}
to take the initial example? If so, you're welcome to re-open...
from magrittr.
Related Issues (20)
- Feature request: Assignment expression operator HOT 1
- Move `master` branch to `main` HOT 1
- `magrittr::freduce()` doesn't support lambda functions HOT 4
- Add warning when piped object overwrites keyword arguments when not used as first argument HOT 1
- Inconsistent assign() behavior in simple piping with Magrittr HOT 1
- Bug: Rstudio completion with "mrgmod" S4 class object HOT 4
- Feature: Function to create functional sequences HOT 1
- Suggest alias for `Encoding<-` HOT 1
- Hartmann pipelines HOT 3
- Magrittr Logo HOT 1
- difference in performance HOT 2
- Different results using pipes HOT 2
- introspection HOT 2
- magrittr upkeep 2023
- Add function not behaving correctly when being fed an expression
- Pipe doesn't work in basic round function HOT 1
- Question: magrittr's pipes not hyperlinked in docs, why not?
- Question: UN or UNE pipe? HOT 1
- Assignment pipe `%<>%` breaks if R-native `|>` pipes used in chain instead of magrittr-native pipes `%>%` HOT 1
- How to quote magrittr library/package in function call: `magrittr::%<>%` doesn't work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magrittr.