Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upCreate backpipe %<% operator #26
Comments
|
This is an interesting example; and I do agree that there are examples where it can be useful. It is not obvious how to incorporate this in the current setup, as the LHS'es are evaluated as the the chain proceeds and altering the call ex-post is not possible (or at least not entirely worth the trouble). But I'll keep it in mind as I am considering new approaches, and whether the entire chain should be considered before any evaluation takes place... Maybe then it can be done in a more natural way. |
|
Also, it is not really clear what shoul happen if you mix the two
|
|
I can't think of a use case in which you would ever think of mixing the two. You can't cross the streams. That would be chaos: Human sacrifice, dogs and cats living together... mass hysteria! |
|
I think fun1 should take the left side pipe as first argument, and the f(arg1, arg2, arg3, arg4)
arg2 %>% f %<% arg3 This way we could express complex expressions as DAGs in a graphical form. Antonio On Fri, Aug 1, 2014 at 9:07 AM, Christopher Brown notifications@github.com
|
|
Hah! maybe you can add a few diagonal outlets to that graph for assignment! :-p |
|
It would be actually cool to be able to have a way to define a DAG for data processing. You could define what the inputs are, and how exactly they flow trough the DAG, the functions at the nodes, which piece will be which argument of the function, etc. Conditional nodes would push the data along different branches, and the data that arrives at the special output node would be the output. We could design some syntax for describing such a DAG! We could call it, ehm, R, maybe? Oh wait |
|
@gaborcsardi we could design some better syntax for the same DAGs, or should we leave all the fun to @smbache?
It's becoming more like compiling ASCII art. Or a cellular automaton. |
|
Maybe I'll leave this to someone who could write a |
|
:) Actually, why stop with DAGs? Let the data flow through loops of pipes, in the circle of life! What is the output then, you might ask. Well, it can be stable attractors, oscillations, other quasi-spatio-temporal patterns. But who cares what the output is, anyway? Who would want to quit such a program? |
|
Despite, you guys getting your yucks about this ... ( looking at you @piccolbo ;) ) There does seem to be some interest in this: http://stackoverflow.com/questions/31305342/is-right-to-left-operator-associativity-in-r-possible |
|
It seems to me that the main point of %>% is how it rearranges tokens so that related things are closer together. I look at @ctbrown example and the one on SO and I don't see any rearrangement.
Order is identical, trading a comma for a %<%, one lowly parens moving a tiny bit. Am I trivializing things, missing the point? Maybe an example with two consecutive uses of the operator levels would help me. Sorry for veering on the comedic side on the DAGs but I was serious about them. I will open another issue if anybody's interested. |
|
Well, the SO example has three or more consecutive uses of %<% or equivalent. |
|
No worries about the comedic side. Exactly! The utility of back-piping becomes more apparent with increased complexity. Compare:
The latter is more legible than the former. While it might be argued that What I might concede is that a backpipe operator does not belong in the Really my only point in posting is to be complete in the documentation of this issue and capture the additional data point of the SO post. |
|
I always write my code that way and I don't have any backpipes! I write my code so that indentation is proportional to depth in the parse tree for the n top levels, and on one line below n, n varies a little bit heuristically, I must admit. I am also thinking about a formatter that automates this approach.
You don't need backpipes to write your code nicely, you just need to underwrite the principles of semantic formatting. Wait a second, I haven't written them down yet ... |
|
Except what you didn't write the same code. You meant:
IMO, this is not as clear as my example, indentation or not.. |
|
I stand corrected. On Fri, Aug 28, 2015 at 12:22 AM Christopher Brown notifications@github.com
|
|
A reverse magrittr would be nice. Here is a StackOverflow related question. |
|
|
Also: I just submitted a package to CRAN called |
|
@ctbrown |
|
The backpipe package has been accepted by CRAN, AFAIAC you can close this issue. |
|
great! |
There are times when it is desirable to reverse the direction of the pipe operator to match the order of parenthesis. The motivating example is a shiny application in which the shiny commands build up HTML. A backpipe operator would preserve the hierarchy of the HTML while cleaning up the code immensely. Consider:
The proposed syntax not only make the code more readable, but alos preserves the hierarchical nature of the HTML so the code more closely matches the generated HTML.
( BTW, I am already doing this with shiny application by simply cloning %>% and reversing the order of the arguments. I have not investigated how this would work with the changes for 1.1.0. I am happy to submit code if @smbache thinks this is a useful feature. )