Simple way to define a function to be used in a formula object inside another function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple way to define a function to be used in a formula object inside another function

Frank Harrell
I would like to do this:

f <- function(formula, data=NULL) {
  gg <- sqrt
  model.frame(formula, data=data)
  }
x <- y <- 1:10
f(y ~ gg(x))
Error in eval(expr, envir, enclos) : could not find function "gg"

Is there a simple way to get access to gg from within the model.frame
invocation inside f?

Thanks
Frank

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Simple way to define a function to be used in a formula object inside another function

William Dunlap
The following works because model.frame looks for things in environment(formula)
and ancestral environments thereof.  It puts the new things in a child environment
of the original environment(formula) so it does not alter the original environment.

f2 <- function (formula, data = NULL)
{
    environment(formula) <- new.env(parent = environment(formula))
    assign(envir = environment(formula), "gg", sqrt)
    model.frame(formula, data = data)
}

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Frank Harrell
> Sent: Sunday, December 15, 2013 10:09 AM
> To: RHELP
> Subject: [R] Simple way to define a function to be used in a formula object inside another
> function
>
> I would like to do this:
>
> f <- function(formula, data=NULL) {
>   gg <- sqrt
>   model.frame(formula, data=data)
>   }
> x <- y <- 1:10
> f(y ~ gg(x))
> Error in eval(expr, envir, enclos) : could not find function "gg"
>
> Is there a simple way to get access to gg from within the model.frame
> invocation inside f?
>
> Thanks
> Frank
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Simple way to define a function to be used in a formula object inside another function

Frank Harrell
In reply to this post by Frank Harrell
Thank you Bill, that worked perfectly.
Frank

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Frank Harrell
Department of Biostatistics, Vanderbilt University