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

How to use logical operator (!) with magrittr in R

I am taking a list of values and trying to find those that are not NA using magrittr. Here is a simple example: data.frame(data = c(1:2, NA, 4:5, NA, 7)) %>% is.na which yields the correct result: data [1,] FALSE [2,] FALSE [3,] TRUE [4,]…
Mark Danese
  • 2,331
  • 1
  • 20
  • 25
10
votes
3 answers

How to pipe purely in base R ('base pipe')?

Is there a way to pipe in base R, without having to define your own function (i.e. something 'out of the box'), and without having to load any external packages? That is, some (base R) alternative to the magrittr pipe %>%. Possibly in a forthcoming…
stevec
  • 41,291
  • 27
  • 223
  • 311
10
votes
2 answers

Using the pipe in unique() function in r is not working

I have some troubles using the pipe operator (%>%) with the unique function. df = data.frame( a = c(1,2,3,1), b = 'a') unique(df$a) # no problem here df %>% unique(.$a) # not working here # I got "Error: argument 'incomparables != FALSE' is not…
Rtist
  • 3,825
  • 2
  • 31
  • 40
10
votes
1 answer

Order of operation with piping

My question is where does the piping operator of magrittr package %>% come in the order of operations? I have a problem simmilar to the following: set.seed(10) df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3)) df/rowSums(df) %>% round(.,3) This…
mallet
  • 2,454
  • 3
  • 37
  • 64
9
votes
2 answers

R package fails devtools::check, because "could not find function" even though the function is imported in NAMESPACE

Trying to build my first R package using roxygen2 and devtools. I have added a function that uses %>% and mutate in the @examples section. When I run check() it fails, because it cannot find the function %>% or mutate. Based on this, this, and this…
Andrew
  • 490
  • 3
  • 9
9
votes
2 answers

Using ggsave with a pipe

I can save a plot with ggsave after I stored it, but using it in a pipeline I get the following error. I wish to plot and save in the same (piped) command. no applicable method for 'grid.draw' applied to an object of class "c('LayerInstance',…
fliestech
  • 165
  • 2
  • 9
9
votes
1 answer

Get expression that evaluated to dot in function called by `magrittr` pipe

I have a function x_expression() which prints the expression passed to argument x. pacman::p_load(magrittr, rlang) x_expression <- function(x) { print(enquo(x)) } y <- 1 x_expression(y) #> #> expr: ^y #> env: global y %>%…
Rory Nolan
  • 972
  • 10
  • 15
9
votes
3 answers

Reason for magrittr pipe (%>%) with ifelse behavior?

I am using ifelse with pipe in a scenario where I use the forwarded result in both the condition and ifelse branches. The below is a simplified version: it seems that ifelse + pipe only handles condition different from . if put inside curly braces. …
Ildi Czeller
  • 190
  • 1
  • 9
9
votes
2 answers

Parallel wilcox.test using group_by and summarise

There must be an R-ly way to call wilcox.test over multiple observations in parallel using group_by. I've spent a good deal of time reading up on this but still can't figure out a call to wilcox.test that does the job. Example data and code below,…
9
votes
1 answer

Is there a way to `pipe through a list'?

One really cool feature from the ggplot2 package that I never really exploited enough was adding lists of layers to a plot. The fun thing about this was that I could pass a list of layers as an argument to a function and have them added to the…
Benjamin
  • 16,897
  • 6
  • 45
  • 65
9
votes
1 answer

Convert column in data.frame to date

My dataframe a1 <- c("a","a","b","b","c","d","e","e") b2 <- c("01.01.2015", "02.02.2015", "14.02.2012", "16.08.2008", "17.06.2003", "31.01.2015", "07.01.2022", "09.05.2001") c3 <- c("1a", "2b", "3c", "4d", "5e", "6f", "7g", "8h") d3 <- c(1:8) df2…
four-eyes
  • 10,740
  • 29
  • 111
  • 220
9
votes
3 answers

How to build a pipeline from data.table to magrittr and back to data.table

I would like to mix data.table pipelining with magrittr pipelining. I can go from data.table to %>% but I can't figure out how to get back to [][] data.table style pipelining. Here's an example: > tbl = data.table(grp=c(1,1,1,2,2,2,3,3,3,4,4),…
Clayton Stanley
  • 7,513
  • 9
  • 32
  • 46
8
votes
1 answer

What are the disadvantages when using a function without empty parentheses with %>%, the pipe of magrittr?

I have used %>%, the magrittr pipe, as given in its documentation by providing a function without empty parentheses to the RHS in this answer and got a comment that the recommended convention is to supply empty parentheses to the…
GKi
  • 37,245
  • 2
  • 26
  • 48
8
votes
1 answer

Meaning of error using . shorthand inside dplyr function

I'm getting a dplyr::bind_rows error. It's a very trivial problem, because I can easily get around it, but I'd like to understand the meaning of the error message. I have the following data of some population groups for New England states, and I'd…
camille
  • 16,432
  • 18
  • 38
  • 60
8
votes
1 answer

Error in magrittr pipe when using ``magrittr::`%>%` ``

For whatever reason I was playing with magrittr pipe syntax, and came across a weird error that occurs when you scope explicitly qualify the call to %>%. I know that using the syntax below destroys the purpose of the pipe, but I'm curious as to why…
Adam Spannbauer
  • 2,707
  • 1
  • 17
  • 27