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
8
votes
1 answer

How do I use magrittr::inset()?

I understand that magrittr::inset() should be able to assign a vector to a new column in a dataframe (as a sort of opposite of extract()). But I don't understand how the syntax should work. Say I've got, as a toy example: df = data.frame( id = 1:26,…
crazybilly
  • 2,992
  • 1
  • 16
  • 42
8
votes
1 answer

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

It seem %>% in the magrittr package is not working for the function load(). This is my minimal example to reproduce my question. ## Create two example variables and save to tempdir() a <- 1 b <- 1 save(list = ls(), file = file.path(tempdir(),…
Bangyou
  • 9,462
  • 16
  • 62
  • 94
7
votes
1 answer

RMarkdown: Wrap code in chunks but keep breaks after pipe

I'm trying to achieve two things that might not be compatible: I would like my code that is in my RMarkdown chunks to be wrapped around in each each line when I create a PDF (in other words, the line below runs over the page edge). After reading…
Moritz Schwarz
  • 2,019
  • 2
  • 15
  • 33
7
votes
1 answer

magrittr pipe not evaluating a dot passed a second pipe within a function argument

When using dot a second time to reuse the data on the left of a pipe, passing the dot to a function . %>% f() is different to putting the dot inside the function brackets f(.). Why is this? Debugging the %>% operator shows that . %>% identity()…
Charlie
  • 188
  • 3
7
votes
1 answer

Build a RStudio addin to debug pipe chains

I wrote a function that helps executing pipe chains step by step. To use it the users has to copy the instruction to clipboard, then execute the function, and move to the console to proceed. I would like to build an addin that would allow me to…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
7
votes
1 answer

How to join a data frame to itself within a dplyr chain?

Occasionally, I need to join a data frame to (usually a modified) version of itself within a dplyr chain. Something like this: df <- data.frame( id = c(1,2,3) , status = c('foo','bar','meh') , spouseid = c(4,3,2) ) df %>% filter(…
crazybilly
  • 2,992
  • 1
  • 16
  • 42
7
votes
3 answers

R: Further subset a selection using the pipe %>% and placeholder

I recently discovered the pipe operator %>%, which can make code more readable. Here is my MWE. library(dplyr) # for the pipe operator library(lsr) # for the cohensD…
piptoma
  • 754
  • 1
  • 8
  • 19
7
votes
1 answer

Pipe a data frame to a function whose argument pipes a dot

How can one pipe a data frame to a function whose argument pipes a dot? mpg %>% rbind(., . %>% rev()) Error in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure' Another example: mpg %>% { . %>%…
Edward R. Mazurek
  • 2,107
  • 16
  • 29
7
votes
2 answers

Combining pipes and the magrittr dot (.) placeholder

I am fairly new to R and I am trying to understand the %>% operator and the usage of the " ." (dot) placeholder. As a simple example the following code works library(magrittr) library(ensurer) ensure_data.frame <-…
7
votes
1 answer

Constructing function from a closure with Magrittr is resulting in an error upon function evaluation

When I use magrittr to pass a value for x to the function below, it results in an unusable function. Why is this happening? I have version magrittr_1.5. library(magrittr) f <- function(x) { function(y) { x + y } } # Case 1 (works) f.5 <-…
Maxwell
  • 71
  • 3
6
votes
1 answer

Print message in the middle of long pipe

In my R script, I would like to print a message in the middle of a long pipe (which takes a long time to run) without breaking the pipe. I need this to track the progress of the running pipeline. I tried to create a simple function that prints a…
Ahmed El-Gabbas
  • 398
  • 3
  • 10
6
votes
2 answers

How can I use a progress bar for piped functions in R / tidyverse

I have a main function which performs a handful of variously complicated (and long-running) computations on some data, it performs these steps using the pipe from tidyverse / magrittr. I would like a progress bar to report on the stage of the…
Moohan
  • 933
  • 1
  • 9
  • 27
6
votes
2 answers

`magrittr` pipe into apply

How do you pipe an object into a specific place in an apply call, that isn't the first input? The magrittr dot placeholder does not seem to work for this. dat <- 1:10 locs <- list(c(1, 2), c(3, 4), c(5, 6)) …
hokeybot
  • 195
  • 5
6
votes
2 answers

Replace df <- df %>% ... with a shortcut

I guess most of us have already used something like this (at least if you are using the tidyverse): library(tidyverse) example <- mtcars example <- example %>% select(- mpg) My question: I know there is a shortcut for this part: example <-…
KevR
  • 131
  • 7
6
votes
4 answers

Merge rows in tibble

I'd like to list all Functions my package in a table. So far I extracted all functions and title from the packages help docs library(magrittr) package_info <- library(help = magrittr)$info[[2]] package_info_tbl <- package_info %>% …
Thomas
  • 1,252
  • 6
  • 24