Re: [Rd] Aliasing a function

2008-02-23 Thread John Fox
Dear Hadley,

Why not just f <- g ?

Regards,
 John


John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> project.org] On Behalf Of hadley wickham
> Sent: February-23-08 5:40 PM
> To: R-devel
> Subject: [Rd] Aliasing a function
> 
> A simple way to alias a function is to do :
> 
> g <- function(a = 1, b = 2, c = 3) a + b * c
> f <- function(...) g(...)
> 
> but formals (etc) is no longer very helpful.  Is there an easy way to
> programmatically create:
> 
> f <- function(a=1, b=2, c=3) g(a=a, b=b, c=c)
> 
> This comes up in ggplot2 where I alias many functions to hide the fact
> that I'm using proto in the background, and I'd like the aliased
> functions to be a little more user friendly in terms of revealing what
> are valid arguments (and also to match up with the rdoc files which I
> am building).
> 
> Any ideas?
> 
> Thanks,
> 
> Hadley
> 
> --
> http://had.co.nz/
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Aliasing a function

2008-02-23 Thread Gabor Grothendieck
formals can be used like this:

f <- g
formals(f) <- pairlist(a=1,b=2,c=3)


On Sat, Feb 23, 2008 at 5:40 PM, hadley wickham <[EMAIL PROTECTED]> wrote:
> A simple way to alias a function is to do :
>
> g <- function(a = 1, b = 2, c = 3) a + b * c
> f <- function(...) g(...)
>
> but formals (etc) is no longer very helpful.  Is there an easy way to
> programmatically create:
>
> f <- function(a=1, b=2, c=3) g(a=a, b=b, c=c)
>
> This comes up in ggplot2 where I alias many functions to hide the fact
> that I'm using proto in the background, and I'd like the aliased
> functions to be a little more user friendly in terms of revealing what
> are valid arguments (and also to match up with the rdoc files which I
> am building).
>
> Any ideas?
>
> Thanks,
>
> Hadley
>
> --
> http://had.co.nz/
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Aliasing a function

2008-02-23 Thread Gabor Grothendieck
I assume he wants to be able to change the
formals although its confusing since the example
uses the same formals in both cases.

On Sat, Feb 23, 2008 at 6:32 PM, John Fox <[EMAIL PROTECTED]> wrote:
> Dear Hadley,
>
> Why not just f <- g ?
>
> Regards,
>  John
>
> 
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada L8S 4M4
> 905-525-9140x23604
> http://socserv.mcmaster.ca/jfox
>
>
>
> > -Original Message-
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > project.org] On Behalf Of hadley wickham
> > Sent: February-23-08 5:40 PM
> > To: R-devel
> > Subject: [Rd] Aliasing a function
> >
> > A simple way to alias a function is to do :
> >
> > g <- function(a = 1, b = 2, c = 3) a + b * c
> > f <- function(...) g(...)
> >
> > but formals (etc) is no longer very helpful.  Is there an easy way to
> > programmatically create:
> >
> > f <- function(a=1, b=2, c=3) g(a=a, b=b, c=c)
> >
> > This comes up in ggplot2 where I alias many functions to hide the fact
> > that I'm using proto in the background, and I'd like the aliased
> > functions to be a little more user friendly in terms of revealing what
> > are valid arguments (and also to match up with the rdoc files which I
> > am building).
> >
> > Any ideas?
> >
> > Thanks,
> >
> > Hadley
> >
> > --
> > http://had.co.nz/
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Aliasing a function

2008-02-23 Thread hadley wickham
On Sat, Feb 23, 2008 at 5:52 PM, Gabor Grothendieck
<[EMAIL PROTECTED]> wrote:
> I assume he wants to be able to change the
>  formals although its confusing since the example
>  uses the same formals in both cases.

Yes, that was an important point that I forgot to mention!  Thanks for
the pointer to formals but it doesn't work in this case:

function (a = 1, b = 2, c = 3)
g(...)
> f(c=5)
Error in f(c = 5) : '...' used in an incorrect context

Hadley


-- 
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Aliasing a function

2008-02-24 Thread Tony Plate
Maybe something like this? (though it seems like it might be more 
straightforward to do this sort of thing by text-processing a source 
file then source'ing it in R).

 > f <- function(a, b, c) c(a=a,b=b,c=c)
 > attr(f, "source") <- NULL
 > f
function (a, b, c)
c(a = a, b = b, c = c)
 > g1 <- f
 > ff <- formals(f)
 > argtrans <- c(a="d", b="e", c="f")
 > names(ff) <- argtrans
 > g2 <- as.function(c(ff, as.call(c(list(as.name("f")), 
lapply(argtrans, as.name)
 > g2
function (d, e, f)
f(a = d, b = e, c = f)
 > f(1,2,3)
a b c
1 2 3
 > g1(a=1,b=2,c=3)
a b c
1 2 3
 > g2(d=1,e=2,f=3)
a b c
1 2 3
 >

-- Tony Plate


hadley wickham wrote:
> On Sat, Feb 23, 2008 at 5:52 PM, Gabor Grothendieck
> <[EMAIL PROTECTED]> wrote:
>   
>> I assume he wants to be able to change the
>>  formals although its confusing since the example
>>  uses the same formals in both cases.
>> 
>
> Yes, that was an important point that I forgot to mention!  Thanks for
> the pointer to formals but it doesn't work in this case:
>
> function (a = 1, b = 2, c = 3)
> g(...)
>   
>> f(c=5)
>> 
> Error in f(c = 5) : '...' used in an incorrect context
>
> Hadley
>
>
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Aliasing a function

2008-02-26 Thread hadley wickham
Thanks Tony (and others), for getting me started.  I eventually ended up with:

TopLevel$build_accessor <- function(., extra_args = c()) {
  layer <- if (.$class() %in% c("geom","stat", "position")) c(
list(mapping=NULL,data=NULL),
compact(list(
  geom = if (exists("default_geom", .)) .$default_geom()$objname,
  stat = if (exists("default_stat", .)) .$default_stat()$objname,
  position = if (exists("default_pos", .)) .$default_pos()$objname
))
  )
  params <- .$params()
  params <- params[names(params) != "..."]
  args <- c(layer, params)

  body <- ps(
.$myName(), "$", "new(",
if (length(args) > 0) ps(names(args),"=", names(args), collase =", "),
if (length(extra_args) > 0) ps(names(extra_args),"=", extra_args,
collase =", "),
"...",
")"
  )
  f <- function() {}
  formals(f) <- as.pairlist(c(args, alist(... =)))
  body(f) <- parse(text = body)
  environment(f) <- globalenv()
  f
}

-- obviously there are far more ggplot details in there than I had
first anticipated.

Hadley

On Mon, Feb 25, 2008 at 12:24 AM, Tony Plate <[EMAIL PROTECTED]> wrote:
> Maybe something like this? (though it seems like it might be more
>  straightforward to do this sort of thing by text-processing a source
>  file then source'ing it in R).
>
>   > f <- function(a, b, c) c(a=a,b=b,c=c)
>   > attr(f, "source") <- NULL
>   > f
>  function (a, b, c)
>
> c(a = a, b = b, c = c)
>   > g1 <- f
>   > ff <- formals(f)
>   > argtrans <- c(a="d", b="e", c="f")
>   > names(ff) <- argtrans
>   > g2 <- as.function(c(ff, as.call(c(list(as.name("f")),
>  lapply(argtrans, as.name)
>   > g2
>  function (d, e, f)
>  f(a = d, b = e, c = f)
>   > f(1,2,3)
>  a b c
>  1 2 3
>   > g1(a=1,b=2,c=3)
>  a b c
>  1 2 3
>   > g2(d=1,e=2,f=3)
>  a b c
>  1 2 3
>   >
>
>  -- Tony Plate
>
>
>
>
>  hadley wickham wrote:
>  > On Sat, Feb 23, 2008 at 5:52 PM, Gabor Grothendieck
>  > <[EMAIL PROTECTED]> wrote:
>  >
>  >> I assume he wants to be able to change the
>  >>  formals although its confusing since the example
>  >>  uses the same formals in both cases.
>  >>
>  >
>  > Yes, that was an important point that I forgot to mention!  Thanks for
>  > the pointer to formals but it doesn't work in this case:
>  >
>  > function (a = 1, b = 2, c = 3)
>  > g(...)
>  >
>  >> f(c=5)
>  >>
>  > Error in f(c = 5) : '...' used in an incorrect context
>  >
>  > Hadley
>  >
>  >
>  >
>
>



-- 
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel