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

How to apply a function on a selection of columns in a pipe sequence in R?

I have a dataframe (or a tibble no matter) with many columns and I want to apply a function (let's say rowSums) on only 7 of them, but I don't want to get reed of the others. The trick is that I want to do so in a pipe sequence - create (or read…
4
votes
1 answer

compatibility issue of magrittr and arima in R

consider the following example: library(tidyverse) set.seed(1) forecast::forecast x <- cumsum(rnorm(10)) y1 <- arima(x, order = c(1, 0, 0)) y2 <- x %>% arima(order = c(1, 0, 0)) length(fitted(y1)) [1] 10 length(fitted(y2)) [1] 0 The objects y1and…
Cettt
  • 11,460
  • 7
  • 35
  • 58
4
votes
1 answer

dplyr Replace values in multiple variables

I need to replace non-4 with 80 in cyl, gear, carb columns. I tried the following, but it does not work. mtcars %>% mutate_at(vars(cyl, gear, carb), replace(which(.!=4), 80)) It throws the following error: Error in replace(which(. != 4), 80) : …
Geet
  • 2,515
  • 2
  • 19
  • 42
4
votes
2 answers

Use $ dollar sign at end of of an R magrittr pipeline to return a vector

I'd like to use $ at the end of a magrittr/tidyverse pipeline. $ works directly next to tidyverse functions like read_csv and filter, but as soon I create a pipeline with %>% it raises an error. Here is a simple reproducible example. # Load…
jmuhlenkamp
  • 2,102
  • 1
  • 14
  • 37
4
votes
3 answers

Use dplyr conditional filter in reactive function in Shiny

I'm using dplyr in a Shiny app reactive function. I have an interactive widget in the UI that users can use to select status and data will be shown based on selected status. However I also want to give the option to show everything, which is a…
Giacomo
  • 1,796
  • 1
  • 24
  • 35
4
votes
1 answer

Referencing piped dataset in ggplot layers for subsetting

Trying to find a way to reference different parts of the dataset for different ggplot2 geom layers without having to save the dataset first in the global environment. Ex non working solution. read_excel("Book1.xlsx",sheet = "Sheet2") %>% …
Mattias W
  • 105
  • 7
4
votes
2 answers

Use purrr::map to apply multiple arguments to a function

I have a data frame like this df <- data.frame(tiny = rep(letters[1:3], 20), block = rnorm(60), tray = runif(60, min=0.4, max=2), indent = sample(0.5:2.0, 60, replace = TRUE)) I nested this data frame nm <-…
Kay
  • 2,057
  • 3
  • 20
  • 29
4
votes
1 answer

R pipe (%>%) capabilities - storage and partial use?

I like the idea of the pipe improving readability, but I've had difficulty using it because of how inflexible it feels. So far I've only succeeded when my goal is to straightforwardly pipe X through a set of functions h(g(f(x,foo),bar),stuff) x…
4
votes
4 answers

Can different parts of dplyr::summarize() be computed conditionally?

Is it possible to have conditional statements operate on different parts of dplyr::summarize()? Imagine I am working with the iris data and outputting a summary and I want to only include the mean of Sepal.Length when requested. So I could do…
Kevin Burnham
  • 553
  • 2
  • 13
4
votes
2 answers

R, dplyr and snow: how to parallelize functions which use dplyr

Let's suppose that I want to apply, in a parallel fashion, myfunction to each row of myDataFrame. Suppose that otherDataFrame is a dataframe with two columns: COLUNM1_odf and COLUMN2_odf used for some reasons in myfunction. So I would like to write…
enneppi
  • 1,029
  • 2
  • 15
  • 33
4
votes
2 answers

Getting system.time or replicate work with piping %>%

I tend to use the piping operator a lot (%>%, from magrittr, or dplyr library). Until one day I tried to use the system.time command on the right-hand-side. system.time(mean(rnorm(1E7))) # ok #### user system elapsed #### 3.52 0.05…
agenis
  • 8,069
  • 5
  • 53
  • 102
4
votes
1 answer

What does the %<>% operator mean in R?

What does the %<>% operator do in R ? What's the difference between using %<>% and <- ? In what type of circumstances %<>% could be useful ?
rafa.pereira
  • 13,251
  • 6
  • 71
  • 109
4
votes
1 answer

Using %>% to specify factor levels on the fly

I am trying to find a one-line option to assign factor levels within a sequence of %>% commands. My strategy for doing this was to run a sequence of functions on . that yields the ordered factor levels I am interested in. This results in "Error:…
shackett
  • 323
  • 2
  • 5
4
votes
1 answer

Why does the following code trying to alias %>% from magrittr symbol fail?

Why does the following code not work? require(dplyr) `%test%`<- `%>%` mtcars %test% head #Error in pipes[[i]] : subscript out of bounds When the following works? a <- function(x) x^2 a(4) #[1] 16 b <- a b(4) #[1] 16 Why does this happen, and what…
Alby
  • 5,522
  • 7
  • 41
  • 51
4
votes
2 answers

Renaming with list with magrittr

I've been playing with magrittr, and I really like the resulting code. It's clean and can really save on typing. How can I rename list elements in magrittr: In typical base R: data_lists <- paste0("q",2011:2015) data_lists <-…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255