Manuel Morales
2006-Oct-27 21:18 UTC
[R] Using data and subset arguments in user-defined functions
Dear list, A while ago, I posted a question asking how to use data or subset arguments in a user-defined function. Duncan Murdoch suggested the following solution in the context of a data argument: data <- data.frame(a=c(1:10),b=c(1:10)) eg.fn <- function(expr, data) { x <- eval(substitute(expr), envir=data) return(mean(x)) } eg.fn(a,data) I've tried various approaches to add a subset argument to the example above, but no luck. I'm looking for something like the following (but that works!) eg.fn2 <- function(expr, data, subset) { data <- subset(data,subset) x <- eval(substitute(expr), envir=data) return(mean(x)) } eg.fn2(a, data, subset=a>3) This returns the error: "Error in eg.fn2(a, data, subset = a > 3) : object "a" not found" Any suggestions? Thanks! -- Manuel A. Morales http://mutualism.williams.edu -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part Url : https://stat.ethz.ch/pipermail/r-help/attachments/20061027/750c2230/attachment.bin
Duncan Murdoch
2006-Oct-27 21:56 UTC
[R] Using data and subset arguments in user-defined functions
On 10/27/2006 5:18 PM, Manuel Morales wrote:> Dear list, > > A while ago, I posted a question asking how to use data or subset > arguments in a user-defined function. Duncan Murdoch suggested the > following solution in the context of a data argument: > > data <- data.frame(a=c(1:10),b=c(1:10)) > > eg.fn <- function(expr, data) { > x <- eval(substitute(expr), envir=data) > return(mean(x)) > } > > eg.fn(a,data) > > I've tried various approaches to add a subset argument to the example > above, but no luck. I'm looking for something like the following (but > that works!) > > eg.fn2 <- function(expr, data, subset) { > data <- subset(data,subset) > x <- eval(substitute(expr), envir=data) > return(mean(x)) > } > > eg.fn2(a, data, subset=a>3) > > This returns the error: > "Error in eg.fn2(a, data, subset = a > 3) : > object "a" not found" > > Any suggestions?The problem is that subset needs to be evaluated with envir=data too. So this works: eg.fn2 <- function(expr, data, subset) { subset <- eval(substitute(subset), envir=data) data <- subset(data,subset) x <- eval(substitute(expr), envir=data) return(mean(x)) } It doesn't work to skip the first assignment and just put it inline into subset(data, eval(substitute(subset), envir=data)) because subset() does nonstandard evaluation of the second argument. Duncan Murdoch
Reasonably Related Threads
- Behavior or as.environment in function arguments/call (and force() behaviors...)
- weights in lm, glm (PR#9023)
- Help with using 'get' function and variable scope
- Arguments to lm() within a function - object not found
- Extract the names of the arguments in an "expression"