Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use magrittr piping with multi-argument functions?

Tags:

r

magrittr

For single argument functions, it is reasonably trivial to translate "standard" R code to the magrittr pipe style.

mean(rnorm(100))

becomes

rnorm(100) %>% mean

For multi-argument functions, it isn't clear to me what the best way to proceed is. There are two cases.

Firstly, the case when additional arguments are constants. In this case, you can create an anonymous function which changes the constant values. For example:

mean(rnorm(100), trim = 0.5)

becomes

rnorm(100) %>% (function(x) mean(x, trim = 0.5))

Secondly, the case where multiple vector arguments are required. In this case, you can combine inputs into a list, and create an anonymous function that operates on list elements.

cor(rnorm(100), runif(100))

becomes

list(x = rnorm(100), y = runif(100)) %>% (function(l) with(l, cor(x, y)))  

In both cases my solutions seem clunky enough that I feel like I'm missing a better way to do this. How should I pipe multiple arguments to functions?

like image 671
Richie Cotton Avatar asked Sep 21 '14 11:09

Richie Cotton


People also ask

What is %>% used for?

The compound assignment %<>% operator is used to update a value by first piping it into one or more expressions, and then assigning the result. For instance, let's say you want to transform the mpg variable in the mtcars data frame to a square root measurement.

What does the Tidyverse pipe operator %>% do?

Use %>% to emphasise a sequence of actions, rather than the object that the actions are being performed on. Avoid using the pipe when: You need to manipulate more than one object at a time. Reserve pipes for a sequence of steps applied to one primary object.

Can you use pipe in a function in R?

The pipe operator is a special operational function available under the magrittr and dplyr package (basically developed under magrittr), which allows us to pass the result of one function/argument to the other one in sequence. It is generally denoted by symbol %>% in R Programming.

What is Magrittr pipe?

The magrittr pipe operators use non-standard evaluation. They capture their inputs and examines them to figure out how to proceed. First a function is produced from all of the individual right-hand side expressions, and then the result is obtained by applying this function to the left-hand side.


1 Answers

In v. 1.5 there are two options:

list(x = rnorm(100), y = runif(100)) %$% cor(x, y) 

Which is essemtially the same as

list(x = rnorm(100), y = runif(100)) %>% with(cor(x, y)) # you could also do this earlier  

Or

list(x = rnorm(100), y = runif(100)) %>% { cor(.$x, .$y) } 

The { pair creates a lambda (unary function) on the fly so you don't have to do the whole (function(x) { ... }) thing.

As a side note, the inset and inset2 aliases can be used to "pick up" values in a pipeline, in e.g. lists.

like image 118
Stefan Avatar answered Nov 15 '22 20:11

Stefan