wolfram mathematica - Is right-to-left operator associativity in R possible? -


i'm new r, , discovered suffer bracket phobia (see comment in link). way magrittr notation %>% works, because avoids nested parenthesis in situations, , makes code more readable. came mathematica, there similar native // notation %>% does. here r , mathematica comparisons:

#r notation     c(1.5,-2.3,3.4) %>% round %>% abs %>% sum   

#mathematica notation {1.5,-2.3,3.4}//round//abs//total 

so far good, but, question is:

is there way mimic mathematica @ notation, right-to-left associativity in r?

here how works in mathematica, solve same code above:

total@abs@round@{1.5,-2.3,3.4} 

in mathematica can write as:

total[abs[round[{1.5,-2.3,3.4}]]] 

just in r be:

sum(abs(round(c(1.5,-2.3,3.4)))) 

but more clean (and cool) have in r this:

sum@abs@round@c(1.5,-2.3,3.4) 

ps: know @ used in s4 classes, , not idea. illustrative comparison.

the backpipe package designed , created purpose. provides backpipe (right-to-left) operator magrittr, piper , forward pipe operator. backpipe can found on github , on cran.

library(magrittr)  # or library(piper) library(backpipe)  x <- c(1.5,-2.3,3.4) sum %<% abs %<% round %<% x  all.equal(                             # true       x %>% round %>% abs %>% sum,       sum %<% abs %<% round %<% x ) 

backpipe not limited additional parameters case of @benbolker's solution. this, example, works backpipe :

mean(na.rm=true) %<% c(1:3,na)   

see discussion on magrittr github issue discuss this.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -