Permalink
Browse files

Here function to help with non-standard eval.

Fixes #3
  • Loading branch information...
1 parent 0f5b667 commit 05cc488447eba138026ccab4dafb781101257de5 @hadley committed Oct 11, 2012
Showing with 65 additions and 0 deletions.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +2 −0 NEWS
  4. +28 −0 R/helper-here.r
  5. +33 −0 man/here.Rd
View
@@ -79,3 +79,4 @@ Collate:
'join_all.r'
'progress-time.r'
'helper-name-rows.r'
+ 'helper-here.r'
View
@@ -50,6 +50,7 @@ export(each)
export(empty)
export(eval.quoted)
export(failwith)
+export(here)
export(id)
export(idata.frame)
export(is.discrete)
View
2 NEWS
@@ -1,6 +1,8 @@
Version 1.7.1.99
------------------------------------------------------------------------------
+* New function `here` makes it possible to use `**ply` + a function that uses non-standard evaluation (e.g. `summarise`, `mutate`, `subset`, `arrange`) inside a function. (Thanks to Peter Meilstrup, #3)
+
* The subsetting in `d*ply` has been considerably optimised: this will have a small impact unless you have a very large number of groups, in which case it will be considerably faster.
* `d*ply` will now preserve factor levels input if `drop = FALSE` (#81)
View
@@ -0,0 +1,28 @@
+#' Capture current evaluation context.
+#'
+#' This function captures the current context, making it easier
+#' to use \code{**ply} with functions that do special evaluation and
+#' need access to the environment where ddply was called from.
+#'
+#' @author Peter Meilstrup, \url{https://github.com/crowding}
+#' @param f a function that does non-standard evaluation
+#' @export
+#' @examples
+#' df <- data.frame(a = rep(c("a","b"), each = 10), b = 1:20)
+#' f1 <- function(label) {
+#' ddply(df, "a", mutate, label = paste(label, b))
+#' }
+#' \dontrun{f1("name:")}
+#' # Doesn't work because mutate can't find label in the current scope
+#'
+#' f2 <- function(label) {
+#' ddply(df, "a", here(mutate), label = paste(label, b))
+#' }
+#' f2("name:")
+#' # Works :)
+here <- function(f) {
+ call <- substitute(function(...) (f)(...), list(f = f))
+ fun <- eval(call, parent.frame())
+ attr(fun, "srcref") <- srcfilecopy("<text>", deparse(call))
+ fun
+}
View
@@ -0,0 +1,33 @@
+\name{here}
+\alias{here}
+\title{Capture current evaluation context.}
+\usage{
+ here(f)
+}
+\arguments{
+ \item{f}{a function that does non-standard evaluation}
+}
+\description{
+ This function captures the current context, making it
+ easier to use \code{**ply} with functions that do special
+ evaluation and need access to the environment where ddply
+ was called from.
+}
+\examples{
+df <- data.frame(a = rep(c("a","b"), each = 10), b = 1:20)
+f1 <- function(label) {
+ ddply(df, "a", mutate, label = paste(label, b))
+}
+\dontrun{f1("name:")}
+# Doesn't work because mutate can't find label in the current scope
+
+f2 <- function(label) {
+ ddply(df, "a", here(mutate), label = paste(label, b))
+}
+f2("name:")
+# Works :)
+}
+\author{
+ Peter Meilstrup, \url{https://github.com/crowding}
+}
+

0 comments on commit 05cc488

Please sign in to comment.