How can I convert R expression into magrittr::piped one automatically

Viewed 51

When you review or debug codes, you come up with codes with nested function calls like f(g(h(2),2,3),4).

We can use magrittr's pipe(%>%) or R base pipe (|>) manually. But what if I want to do it automatically because humans err.

I know of pipefittr but it is not perfect as you can see below.

pipefittr("c(3,2,4,c(3,2,4))")
> 3 %>% 3,2,4,c(2) %>% c()

If the arguments are all unique, then it might need something to start with.

pipefittr("c(1,2,3,c(4,5,6))", start="4")
> 4 %>% c(.,5,6) %>% c(1,2,3,.)

and just omitting . in the first argument would do.

So how can I do this?

0 Answers
Related