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
27
votes
3 answers

Should I avoid programming packages with pipe operators?

Are there any objective reasons for why pipe operators from the R package magrittr, such as %>%, should be avoided when I program packages in R? More specifically, I want to know if using pipe operators might cause coding conflicts or (positively or…
Johan Larsson
  • 3,496
  • 18
  • 34
23
votes
1 answer

Order of operations in summarise

What is happening in the first line of code and why does the result differ from the two next results? library(tidyverse) library(magrittr) data.frame(A=c(2,2),B=c(1,1)) %>% summarise(A = sum(A),B = sum(B), D=sum(A)-sum(B)) yields…
Martin
  • 331
  • 2
  • 10
23
votes
6 answers

Adding prefix or suffix to most data.frame variable names in piped R workflow

I want to add a suffix or prefix to most variable names in a data.frame, typically after they've all been transformed in some way and before performing a join. I don't have a way to do this without breaking up my piping. For example, with this…
Sam Firke
  • 21,571
  • 9
  • 87
  • 105
23
votes
5 answers

how to feed the result of a pipe chain (magrittr) to an object

This is a fairly simply question. But I couldn't find the answer per google/stackexchange and looking at the documentation of magrittr. How do you feed the result of a chain of functions which are connected via %>% to create a vector? what I saw…
grrgrrbla
  • 2,529
  • 2
  • 16
  • 29
23
votes
2 answers

Differences between %.% (dplyr) and %>% (magrittr)

The dplyr package introduced the %.% operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. The magrittr package is a much more lightweight package that exists to define only that…
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
22
votes
2 answers

dplyr . and _no visible binding for global variable '.'_ Note in package check

In dplyr one can write code like e.g. using the '.' to refer to the data in the pipe x <- data.frame(x = 2:4) y <- data.frame(y = 1:3) y %>% dplyr::bind_cols(x,.) but when using it in a function and running the package check it produces the no…
witek
  • 984
  • 1
  • 8
  • 25
22
votes
3 answers

Use pipe without feeding first argument

Is the %>% pipe operator always feeding the left-hand side (LHS) to the first argument of the right-hand side (RHS)? Even if the first argument is specified again in the RHS call? Say I want to specify which variable to use in cor():…
Matifou
  • 7,968
  • 3
  • 47
  • 52
21
votes
3 answers

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

I'm new to R, and I just discovered I suffer from Bracket Phobia (see comment in the link). I like the way magrittr notation %>% works, because it avoids nested parenthesis in some situations, and makes code more readable. I came from Mathematica,…
Murta
  • 2,037
  • 3
  • 25
  • 33
20
votes
2 answers

Should I use %$% instead of %>%?

Recently I have found the %$% pipe operator, but I am missing the point regarding its difference with %>% and if it could completely replace it. Motivation to use %$% The operator %$% could replace %>% in many cases: mtcars %>% summary() mtcars…
Gorka
  • 1,971
  • 1
  • 13
  • 28
20
votes
4 answers

Why is using dplyr pipe (%>%) slower than an equivalent non-pipe expression, for high-cardinality group-by?

I thought that generally speaking using %>% wouldn't have a noticeable effect on speed. But in this case it runs 4x slower. library(dplyr) library(microbenchmark) set.seed(0) dummy_data <- dplyr::data_frame( id=floor(runif(10000, 1, 10000)) …
logworthy
  • 1,218
  • 2
  • 11
  • 23
19
votes
1 answer

suppressWarnings() doesn't work with pipe operator

I am trying to suppress warnings by using the suppressWarnings() function. Surprisingly, it removes warnings when used normally, but it fails to do so when you use the pipe %>% operator. Here is some example code : library(magrittr) c("1", "2",…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
18
votes
4 answers

Custom pipe to silence warnings

Related to this question. I'd like to build a custom pipe %W>% that would silence warnings for one operation library(magrittr) data.frame(a= c(1,-1)) %W>% mutate(a=sqrt(a)) %>% cos will be equivalent to : w <- options()$warn data.frame(a= c(1,-1))…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
18
votes
3 answers

Conditional dataframe mutations in R with magrittr and dplyr

I would like to use the succinctness of magrittr and dplyr to copy single values between rows in a subset of columns based on the values in other columns. This is a simple example; I want to apply this idea to many columns of a large dataset with…
18
votes
5 answers

Stepping through a pipeline with intermediate results

Is there a way to output the result of a pipeline at each step without doing it manually? (eg. without selecting and running only the selected chunks) I often find myself running a pipeline line-by-line to remember what it was doing or when I am…
andrew wong
  • 827
  • 1
  • 7
  • 9
16
votes
2 answers

R piping (%>%) does not work with replicate function

I am trying to learn the piping function (%>%). When trying to convert from this line of code to another line it does not work. ---- R code -- original version ----- set.seed(1014) replicate(6,sample(1:8)) [,1] [,2] [,3] [,4] [,5] [,6] [1,] …
phage
  • 584
  • 3
  • 17
1
2
3
31 32