Skip to content

Commit

Permalink
Import %>% from magrittr. Closes #330
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 14, 2014
1 parent 8139dbf commit 89aaa9a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Imports:
assertthat,
utils,
methods,
Rcpp
Rcpp,
magrittr
Suggests:
hflights,
RSQLite,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ S3method(union,default)
S3method(unique,sql)
S3method(update,tbl_sql)
export("%.%")
export("%>%")
export(.datatable.aware)
export(Progress)
export(anti_join)
Expand Down
20 changes: 20 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# dplyr 0.1.3.0.99

* dplyr now imports `%>%` from magrittr (#330). I recommend that you use this
instead of `%.%` because it is easy to type (since you can hold down the
shift key) and is more flexible. With you `%>%`, you can control which
argument on the RHS recieves the LHS, by using the pronoun `.`. This makes
`%>%` more useful with base R functions because they don't always take the
data frame as the first argument. For example you could pipe `mtcars` to
`xtabs()` with:

```R
mtcars %>% xtabs( ~ cyl + vs, data = .)
```
Thanks to @smbache for the excellent margrittr package. I recommend
reading `vignette("magrittr")` to learn more. dplyr only provides `%>%`
from magrittr. If you want to use the other useful functions that it
provides, you'll need to load it explicitly with `library(magrittr)`.

`%.%` will be deprecated in a future version of dplyr, but it won't
happen for a while.

* `do()` has been completely overhauled to be more useful. There are now
two ways to use it - either with named or unnamed arguments. If you have
named arguments, you'll get one column in the output for each named
Expand Down
11 changes: 8 additions & 3 deletions R/chain.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' 0.3. It was removed in the interest of making dplyr code more
#' standardised and \code{\%.\%} is much more popular.
#'
#' @param x,y A dataset and function to apply to it
#' @param lhs,rhs A dataset and function to apply to it
#' @param ...,calls A sequence of data transformations, starting with a dataset.
#' The first argument of each call should be omitted - the value of the
#' previous step will be substituted in automatically. Use \code{chain} and
Expand Down Expand Up @@ -91,6 +91,11 @@ chain_q <- function(calls, env = parent.frame()) {

#' @export
#' @rdname chain
"%.%" <- function(x, y) {
chain_q(list(substitute(x), substitute(y)), env = parent.frame())
"%.%" <- function(lhs, rhs) {
chain_q(list(substitute(lhs), substitute(rhs)), env = parent.frame())
}

#' @export
#' @rdname chain
`%>%` <- magrittr::`%>%`

7 changes: 5 additions & 2 deletions man/chain.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
% Generated by roxygen2 (4.0.0): do not edit by hand
\name{chain}
\alias{\%.\%}
\alias{\%>\%}
\alias{chain}
\alias{chain_q}
\title{Chain together multiple operations.}
Expand All @@ -9,10 +10,12 @@ chain(..., env = parent.frame())

chain_q(calls, env = parent.frame())

x \%.\% y
lhs \%.\% rhs

lhs \%>\% rhs
}
\arguments{
\item{x,y}{A dataset and function to apply to it}
\item{lhs,rhs}{A dataset and function to apply to it}

\item{...,calls}{A sequence of data transformations, starting with a dataset.
The first argument of each call should be omitted - the value of the
Expand Down

0 comments on commit 89aaa9a

Please sign in to comment.