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

Question: defining a pipe operator with equivalent behavior to %>% {...} #272

Closed
ramiromagno opened this issue Jun 12, 2024 · 2 comments
Closed

Comments

@ramiromagno
Copy link

Hi!

I know that if the placeholder is only used in a nested function call, the lhs is also placed as the first argument, and we can override this by wrapping the rhs in braces. However, I'm wondering if there's a straightforward way, using magrittr's exported functions, to create a new pipe operator that allows multiple placements but never places the lhs as the first argument.

In other words, would it be easy to define a new pipe, say %>>%, that makes these two commands equivalent?

1:10 %>% {c(min(.), max(.))}
1:10 %>>% c(min(.), max(.))

I'm not suggesting that this functionality should be included in magrittr, as I understand the current design choices are deliberate. I'm just asking if I wanted this functionality in my own code, would it be straightforward to implement using one of the exported functions from magrittr?

Thanks!

@lionel-
Copy link
Member

lionel- commented Jun 12, 2024

Untested but try something along the lines of:

`%>>%` <- function(data, expr) {
  eval(substitute(expr), list(. = data), parent.frame())
}

@ramiromagno
Copy link
Author

Thank you Lionel! This is neat and effective!

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

No branches or pull requests

2 participants