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
16
votes
5 answers

How do you end a pipe with an assignment operator?

I want to end a pipe with an assignment operator in R. my goal (in pseudo R): data %>% analysis functions %>% analyzedData where data and analyzedData are both a data.frame. I've tried a few variants of this, each giving a unique error message.…
t-kalinowski
  • 1,420
  • 11
  • 21
15
votes
0 answers

When should we use curly brackets { } when piping with dplyr

I found out that some expressions can only be piped if inside curly brackets (braces, { }), for instance: library(dplyr) 3 %>% {3 + .} 3 %>% {ifelse(. < 2, TRUE, FALSE)} What are the rules behind the use of curly brackets when piping? When should…
mat
  • 2,412
  • 5
  • 31
  • 69
15
votes
1 answer

What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr?

What is the difference between the "+" operator in ggplot2 and the "%>%" operator in magrittr? I was told that they are the same, however if we consider the following script. library(magrittr) library(ggplot2) # 1. This works ggplot(data =…
Anthony Ebert
  • 675
  • 14
  • 25
15
votes
1 answer

How can I make vim indent dplyr code with the pipe (%>%) operator correctly?

For example, vim will not indent correctly the following code: flights <- flights %>% group_by(year, month, day) %>% select(arr_delay, dep_delay) %>% summarise( arr = mean(arr_delay, na.rm = TRUE), dep = mean(dep_delay,…
enricoferrero
  • 2,249
  • 1
  • 23
  • 28
15
votes
1 answer

How to do multiplication with magrittr pipes

A traditional way of making a table in R: data(mtcars) round(100*prop.table(xtabs(~ gear + cyl, data = mtcars), 1), 2) returns cyl gear 4 6 8 3 6.67 13.33 80.00 4 66.67 33.33 0.00 5 40.00 20.00 40.00 To replicate this using…
tomw
  • 3,114
  • 4
  • 29
  • 51
15
votes
3 answers

How to use magrittr piping with multi-argument functions?

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. …
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
15
votes
2 answers

rollmean with dplyr and magrittr

Given the following data: set.seed(1) data <- data.frame(o=c('a','a','a','a','b','b','b','b','c','c','c','c'), t=c(1,2,3,4,1,2,3,4,1,2,3,4), u=runif(12), v=runif(12)) data o t u v 1 a 1 0.26550866 0.6870228 …
JerryWho
  • 3,060
  • 6
  • 27
  • 49
14
votes
4 answers

Using table() in dplyr chain

Can someone explain why table()doesn't work inside a chain of dplyr-magrittr piped operations? Here's a simple reprex: tibble( type = c("Fast", "Slow", "Fast", "Fast", "Slow"), colour = c("Blue", "Blue", "Red", "Red", "Red") ) %>%…
RobertMyles
  • 2,673
  • 3
  • 30
  • 45
14
votes
2 answers

Get name of dataframe passed through pipe in R

I would like to be able to print the name of a dataframe passed through the pipe. Is this possible? I can do. printname <- function(df){ print(paste(substitute(df))) } printname(mtcars) #[1] "mtcars" However, it returns "." when this function…
Ryan Knight
  • 1,288
  • 1
  • 11
  • 18
14
votes
1 answer

Order of execution of nested functions in dplyr pipe

When I use nested function in a piping step, the order of execution seems unintuitive. df <- data.frame(a = c(1,NA,2), b = c(NA, NA, 1)) df %>% is.na %>% colSums # Produce correct count of missing values df %>% colSums(is.na(.)) # Produce NA Can…
Heisenberg
  • 8,386
  • 12
  • 53
  • 102
14
votes
1 answer

What is the difference between %>% and %,% in magrittr?

Github developmental version of magrittr includes some cool new function for piping but I do not exactly catch de difference between %>% and %,%. Is this only formal with %>% for value and %,% for functions, or there is some specific peculiarity?
BBrill
  • 1,922
  • 17
  • 17
12
votes
3 answers

Printing intermediate results without breaking pipeline in tidyverse

Is there a command to add to tidyverse pipelines that does not break the flow, but produces some side effect, like printing something out. The usecase I have in mind is something like this. In case of a pipeline data %>% mutate(new_var =
Raivo Kolde
  • 729
  • 6
  • 14
11
votes
3 answers

R combinations with dot ("."), "~", and pipe (%>%) operator

I have been looking to a lot of answers and still I can't completely understand them. For example, the clearest one (here), among others (1,2,3) gives specific examples about the various uses of the dot but I cannot understand, for example, its…
Chris
  • 2,019
  • 5
  • 22
  • 67
11
votes
3 answers

setNames equivalent for colnames and rownames for use in pipe

I often use R's setNames function in a magrittr pipeline or elsewhere to fix the names of an object on the fly: library(magrittr) mytable %>% setNames(c("col1", "col2", "col3")) %>% ...[more analysis] Are there equivalent functions for colnames and…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
11
votes
3 answers

Multiple ggplots with magrittr tee operator

I am trying to figure out why the tee operator, %T>%, does not work when I pass the data to a ggplot command. This works fine library(ggplot2) library(dplyr) library(magrittr) mtcars %T>% qplot(x = cyl, y = mpg, data = ., geom = "point") %>% …
Tim Cameron
  • 898
  • 6
  • 9
1 2
3
31 32