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

tryCatch() or exists() in R pipe chain error handling

I am currently experiencing a hiccup where I am trying to create a table of values from some upstream calculations. Typically, I've been assuming that there would be at least one value of 1 each time these data frames are created; but I've…
deeboeh
  • 91
  • 8
3
votes
2 answers

What's happening behind the conversion of factor to numeric?

Here is a small data.frame: e = data.frame(A=c(letters[1:5], 1:5)) I am a little bit confused regarding what's happening when I execute the following command: unclass(e$A) %>% as.numeric() I am getting the following output: [1] 6 7 8 9 10 1…
dondapati
  • 829
  • 6
  • 18
3
votes
2 answers

Why does log applied to a vector with a magrittr pipe give unexpected & incorrect values?

I am trying to compute the entropy of a discrete distribution and I noticed the behavior using magrittr is not what I expected. To give an example: > x <- c("A","B","C","A","A","D") …
John Horton
  • 4,122
  • 6
  • 31
  • 45
3
votes
3 answers

How to override magrittr pipe operator?

Say I have a data set and I'd like to apply several filters to it using piping syntax, like so: library(magrittr) library(dplyr) mtcars %<>% filter(cyl == 4) %>% select(cyl, mpg) nrow(mtcars) #[1] 11 I check the current status of the data set…
tonytonov
  • 25,060
  • 16
  • 82
  • 98
3
votes
1 answer

Magrittr forward pipe fails to forward values into openXL::addWorksheet - "Error ...: First argument must be a Workbook"

magrittr appears to be failing to pipe 'workbook' class objects into the addWorkbook function from the package openxlsx. (Never mind why I need to use excel...eugh yuk) For example, to write the InsectSprays dataset to an excel file in 'base'…
Scransom
  • 3,175
  • 3
  • 31
  • 51
3
votes
2 answers

How to use `package::function()` inside a package when `function` is an infix operator?

According to H. Wickham's book R Packages, in the Package Metadata chapter, on how to add a package dependency, Hadley points out good reasons to explicitly refer to external functions using the syntax package::function(). Adding a package…
Ramiro Magno
  • 3,085
  • 15
  • 30
3
votes
1 answer

How to pipe an output tibble into further calculations without saving the tibble as a separate object in R?

I am having a hard time manipulating a tibble output that I receive after piping (using dplyr pipe %>%) a data frame through a series of steps. This code below returns a 2 x 3 tibble ouput: sr_df %>% group_by(ResolutionViolated) %>% tally() %>%…
3
votes
1 answer

inconsistent outcomes when using magrittr and negation operator

> v <- c(1,2,NA,5) > is.na(v) [1] FALSE FALSE TRUE FALSE > !is.na(v) [1] TRUE TRUE FALSE TRUE > > !is.na(v) %>% all() [1] TRUE > all(!is.na(v)) [1] FALSE > (!is.na(v)) %>% all() [1] FALSE In the absence of parenthesis, %>% is applying all() to…
chungkim271
  • 927
  • 1
  • 10
  • 20
3
votes
2 answers

R-devel: "object 'checkCompilerOptions' not found" when installing magrittr

I've installed R-devel on Ubuntu following this guide. When trying to install magrittr with install.packages("magrittr") I'm getting this: * installing *source* package ‘magrittr’ ... ** package ‘magrittr’ successfully unpacked and MD5 sums…
L42
  • 3,052
  • 4
  • 28
  • 49
3
votes
1 answer

Capturing object's "safe state" within pipe chain

I'm running some scientific experiments which involve quite a lot of trial and error. What I basically do is pass some object along a chain of numerical methods with a bunch of parameters on each stage like so: x <- define_object(param1, param2)…
tonytonov
  • 25,060
  • 16
  • 82
  • 98
3
votes
2 answers

Using magrittr and which

I want to rewrite the following code with pipes from magrittr: max(diff(which(diff(runif(50)) > 0 ))) My straightforward approach would be: library(magrittr) runif(50) %>% diff > 0 %>% which %>% diff %>% max But this fails due to the (first)…
mondano
  • 827
  • 10
  • 29
3
votes
4 answers

What's a better way to summarize this data frame?

I have a data frame like this: message.id sender recipient 1 1 A B 2 1 A C 3 2 A B 4 3 B C 5 3 B D 6 3 B Q I would…
Thalecress
  • 3,231
  • 9
  • 35
  • 47
3
votes
2 answers

Adding a variable to a list of data.frames using magrittr syntax

Say you have a list of data.frames that already exist in the environment: library(magrittr) lapply( paste0("z", 2011:2015), function(x) assign( x, data.frame(x=rnorm(10),y=rnorm(10)), pos = 1 ) ) # should create z2011 through…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
3
votes
1 answer

Using magrittr to change a subset of values

I have a time series dataframe with a day of the week column. I would like to replace all of the Mondays (day 1) that are holidays with a 6 for Sunday without breaking my pipeline using magrittr. Without pipelines it looks like this: dates =…
Emrb
  • 75
  • 7
3
votes
1 answer

get the name of variable returned by R function

I have this code: library(magrittr) a <- function(cars) return(cars) b <- function(x) return(list(varname = deparse(substitute(x)), content = x)) b(cars) returns a list with the string cars and the content of the data.frame cars. Any way to get…
user3874377
  • 255
  • 3
  • 10