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

piping with dot inside dplyr::filter

I'm struggling to pipe stuff to another argument inside the function filter from dplyr using %>% margritr. I would assume that this should work: library(dplyr) library(margritr) d <- data.frame(a=c(1,2,3),b=c(4,5,6)) c(2,2) %>% filter(d, a %in%…
Bruno
  • 115
  • 1
  • 9
5
votes
2 answers

Get index of a specific element in vector using %>% operator

I want the index of an element in a vector x x <- c("apple", "banana", "peach", "cherry") With base R i would do that which(x == "peach") But as my x is at the end of a pipe I would like to get the index the magrittr way. x %>%…
Roccer
  • 899
  • 2
  • 10
  • 25
5
votes
1 answer

Magrittr pipe in R functions

Are there cases where it is not advantageous to use the magrittr pipe inside of R functions from the perspectives of (1) speed, and (2) ability to debug effectively?
user2506086
  • 503
  • 2
  • 10
5
votes
2 answers

R: Using piping to pass a single argument to multiple locations in a function

I am attempting to exclusively use piping to rewrite the following code (using babynames data from babynames package: library(babynames) library(dplyr) myDF <- babynames %>% group_by(year) %>% summarise(totalBirthsPerYear = sum(n)) slice(myDF,…
user7988855
  • 95
  • 1
  • 3
5
votes
3 answers

Rename multiple variables within a pipeline

The pipeline metaphor enabled by packages like dplyr and magrittr is incredibly useful and does great things for making your code readable in R (a daunting task!) How can one make a pipeline that ended with renaming all the variables in a data frame…
David M. Perlman
  • 4,851
  • 2
  • 16
  • 12
5
votes
1 answer

Oddity with dplyr and all

I can't figure this out. library(dplyr) dat <- data.frame(a = 1:5,b = rep(TRUE,5)) # this doesn't work dat %>% all(.$b) # tricky # this doesn't work dat %>% all(b) # # this does dat %>% .$b %>% all I find it confusing that all(.$b) doesn't…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
5
votes
2 answers

How can you tell if a pipe operator is the last (or first) in a chain?

I recently have been playing with creating my own pipes, using the awesome pipe_with() function in magittr. I am looking to track the number of pipes in the current chain (so my pipe can behave differently depending on its position in a chain). I…
ecologician
  • 473
  • 3
  • 9
4
votes
2 answers

R: transition from magrittr to native pipe and translation of a function

Please have a look at the reprex at the end of the post. For various reasons, I am transitioning from %>% to the native pipe. I struggle a bit sometimes and I need some comments on a couple of functions. In the first case (complete_data() function…
larry77
  • 1,309
  • 14
  • 29
4
votes
3 answers

Piping into `if` returns the pipeline without evaluating it

I am trying to implement a pipeline that has an optional step which consists of a pipeline of several functions. It runs this pipeline based on a condition, and otherwise it just passes through the original value. However I've tried implementing…
Migwell
  • 18,631
  • 21
  • 91
  • 160
4
votes
1 answer

How do I use subset in a ggplot pipe?

I am trying to use ggplot with subsets to make individually-styled lines for different values of MyName. This works if I set up the data frame as a temporary variable temp that gets referred to in the subset function like temp <- data.frame(x = ...,…
user1683586
  • 373
  • 2
  • 12
4
votes
1 answer

Use argument place holder within ggplot

I am trying to use an argument place holder . within a ggplot(). But it doesn't work for some reason I am not entirely sure of. What I am doing is this (using the sample data from ggplot2/the…
BeSeLuFri
  • 623
  • 1
  • 5
  • 21
4
votes
1 answer

Why do these operations not yield the same result? Piping into . (dot)

I ran into something today when using . and %>% which I don't quite understand. Now I am not sure if I understand either operator. Data set.seed(1) df <- setDT(data.frame(id = sample(1:5, 10, replace = T), value = runif(10))) Why are are these…
Croote
  • 1,382
  • 1
  • 7
  • 15
4
votes
3 answers

Strange behaviour when piping to return() in R function?

Under certain circumstances, piping to return() doesn't appear to behave expectedly. To demonstrate, here are 4 cases Suppose we define a function that returns the result of str_replace_all library(stringr) library(dplyr) string <- letters[1:9] %>%…
stevec
  • 41,291
  • 27
  • 223
  • 311
4
votes
1 answer

Passing an argument twice with magrittr pipe-forward operator

Here's a dummy example of what bothers me (in a vanilla session): library(magrittr) "test" %>% is.na() #[1] FALSE "test" %>% nchar()>3 #[1] TRUE "test" %>% is.na(.) #[1] FALSE "test" %>% nchar(.)>3 #[1] TRUE "test" %>% is.na(.) || nchar(.)>3 #Error…
Vongo
  • 1,325
  • 1
  • 17
  • 27
4
votes
1 answer

Transform data to data.frame with the pipe operator

Lets say i have the following data: x <- 1:2. My desired output is a data.frame() like the following: a b 1 2 With base R i would do something along: df <- data.frame(t(x)) colnames(df) <- c("a", "b") Question: How would i do this with the pipe…
Tlatwork
  • 1,445
  • 12
  • 35