Questions tagged [magrittr]

The magrittr package provides operators for chaining R expressions with forward pipes. Do not use this tag for questions which merely contain a pipe operator. Use this tag for questions asking specifically about the behavior of the %>%, %<>%, %$%, or %T>% operators or the convenience functions provided by the package.

To archive its humble aims, magrittr provides a new “pipe”-like operator for , %>%, with which you may pipe a value forward into an expression or function call; something along the lines of x %>% f, rather than f(x). This is not an unknown feature elsewhere; a prime example is the |> operator used extensively in (to say the least) and indeed this – along with Unix pipes – served as a motivation for developing the magrittr package.

In order to get better answers always use this tag together with .

Development version of magrittr is hosted on GitHub.

477 questions
6
votes
3 answers

R: Using pipe %>% and pkg::fo leads to error "Error in .::base : unused argument"

I am using magrittr's pipe %>%, followed immediately by a function called by: package::function, and get the error: Error in .::base : unused argument (mean) What is the problem? library(magrittr) c(1,2) %>% base::mean #> Error in .::base: unused…
Matifou
  • 7,968
  • 3
  • 47
  • 52
6
votes
1 answer

What does the magrittr dot/period (".") operator do when it's at the very beginning of a pipeline?

I don't understand what the . in the following code is doing or where to find documentation for it: library(tidyverse) ggplot(iris) + geom_point( aes(x=Sepal.Length, y=Sepal.Width), data = . %>% filter(Species == 'setosa') ) This…
nicolaskruchten
  • 26,384
  • 8
  • 83
  • 101
6
votes
1 answer

Pipe in magrittr package is not working for function rm()

x = 10 rm(x) # removed x from the environment x = 10 x %>% rm() # Doesn't remove the variable x 1) Why doesn't pipe technique remove the variable? 2) How do I alternatively use pipe and rm() to remove a variable? Footnote: This question is…
Ashrith Reddy
  • 1,022
  • 1
  • 13
  • 26
6
votes
2 answers

how to transform a string into a factor and sets contrasts using dplyr/magrittr piping

i have a rather specific question: how can I make a string into a factor and set its contrasts within a pipe ? Let's say that I have a tibble like the following tib <- data_frame (a = rep(c("a","b","c"),3, each = T), val = rnorm(9)) Now, I could…
Federico Nemmi
  • 167
  • 1
  • 8
6
votes
4 answers

How to add a row names to a data frame in a magrittr chain

I want to do the opposite of: Convert row names into first column Somewhere down the chain of pipes I would like to add row names to the data frame, for example, I would like to do the following actions using pipes: rownames(mtcars) <-…
Alex
  • 15,186
  • 15
  • 73
  • 127
6
votes
3 answers

Use of ! (or any logical operator) with %>% (magrittr) produces unexpected output

I have run across a situation where %>% produces very surprising output when combined with !. Consider the following code: x <- c(1:20) y <- !is.na(x) > y [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE…
John Paul
  • 12,196
  • 6
  • 55
  • 75
6
votes
3 answers

How can I use dplyr/magrittr's pipe inside functions in R?

I'm trying to write a function which takes as argument a dataframe and the name of the function. When I try to write the function with the standard R syntax, I can get the good result using eval and substitute as recommanded by @hadley in…
PAC
  • 5,178
  • 8
  • 38
  • 62
6
votes
1 answer

Have to call variable twice before evaluated?

Something really odd is going on here. In the code below, I create a variable called temp. I have to call it twice before I can see what it is. E.g. The first time I call it, the console shows nothing. The second time it shows the…
jks612
  • 1,224
  • 1
  • 11
  • 20
6
votes
1 answer

Easily finding and replacing every match in a nested list

Take this object as an example: expr <- substitute(mean(exp(sqrt(.)), .)) It is a nested list. I want to find every element that matches quote(.). For example, magrittr's solution matches only the first level of the call: dots <- c(FALSE,…
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
6
votes
2 answers

passing function argument to dplyr select

To select a couple columns from a dataframe I can do require(dplyr) require(magrittr) df <- data.frame(col1=c(1, 2, 3), col2=letters[1:3], col3=LETTERS[4:6]) df %>% select(col1, col2) I want to write a function similar to f <- function(data,…
kevinykuo
  • 4,600
  • 5
  • 23
  • 31
5
votes
2 answers

Unable to install magrittr

Got a new company laptop and tried to install tidyr or installr, but it always fails because of magrittr. If I want to install this package directly I'm getting the following error code: * installing *source* package 'magrittr' ... ** package…
Marco_CH
  • 3,243
  • 8
  • 25
5
votes
1 answer

How to get a function input name in R whilst using pipes

I am trying to capture the name of the input variable as a character string. This is fairly easy using match.call() however this doesn't work when using magrittr's pipes. Wondering if there is any easy modification that would get it to work within…
gowerc
  • 1,039
  • 9
  • 18
5
votes
3 answers

Choose a function from a tibble of function names, and pipe to it

Let's say I have a tibble (or data frame, whatever) with a list of function names. Let's say, something like: functions <- tibble(c("log()", "log10()", "sqrt()")) I want to be able to pipe a data set to one of these functions, chosen by index. For…
Mooks
  • 593
  • 4
  • 12
5
votes
1 answer

Using `%>%` with `lm` and `rbind`

I have a dataframe Z looking like t x y d 0 1 2 1 1 2 3 1 2 3 4 1 0 1 2 2 1 2 3 2 2 3 4 2 with d being a factor column. I know want to fit a linear model with lm to y over t for both factors in d and add it as a new column to…
Pascal
  • 563
  • 1
  • 3
  • 15
5
votes
1 answer

use magrittr pipe within closures

1 Lets look at this example: 1:3 %>% rep(.,2) + 1 %>% sum #[1] 2 3 4 2 3 4 [2] What R is doing is: 1:3 %>% rep(.,2) + (1 %>% sum) [3] What I want R to do is: (which gives an error) , I like to get 18 there. 1:3 %>% (rep(.,2) + 1) %>% sum #Error…
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69