Re: [Rd] as.function()

2008-01-14 Thread Gabor Grothendieck
The gsubfn package can do something like that too. If you preface a function with fn$ then it will interpret certain formula arguments as functions. If all we want is the function itself we can use force, the identity function, to recover it: > library(gsubfn) > fn$force(~ 2*x + 3*y^2) function

Re: [Rd] as.function()

2008-01-14 Thread Tony Plate
How about this as a version that automatically constructs the argument list (and make into a method for as.function as appropriate)? makefun <- function(expr) { f <- function() {} body(f) <- expr vars <- all.vars(expr) if (length(vars)) { args <- alist(x=)[rep(1,length(va

Re: [Rd] as.function()

2008-01-14 Thread Gabor Grothendieck
On Jan 14, 2008 6:50 AM, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > Robin Hankin wrote: > > Hi > > > > [this after some considerable thought as to R-help vs R-devel] > > > > > > > > I want to write a (S3) method for as.function(); > > toy example follows. > > > > Given a matrix "a", I need to eval

Re: [Rd] as.function()

2008-01-14 Thread Prof Brian Ripley
On Mon, 14 Jan 2008, Duncan Murdoch wrote: > Robin Hankin wrote: >> Hi >> >> [this after some considerable thought as to R-help vs R-devel] >> >> >> >> I want to write a (S3) method for as.function(); >> toy example follows. >> >> Given a matrix "a", I need to evaluate trace(ax) as a function of >

Re: [Rd] as.function()

2008-01-14 Thread Robin Hankin
On 14 Jan 2008, at 11:50, Duncan Murdoch wrote: > Robin Hankin wrote: >> Hi >> [snip] >> a <- crossprod(matrix(rnorm(12),ncol=3)) >> class(a) <- "foo" >> >> f <- as.function(a) # need help to write as.function.foo() >> x <- diag(3) >> >> f(x) #should give tr(ax) >> >> a <- 4 >

Re: [Rd] as.function()

2008-01-14 Thread Duncan Murdoch
Robin Hankin wrote: > Hi > > [this after some considerable thought as to R-help vs R-devel] > > > > I want to write a (S3) method for as.function(); > toy example follows. > > Given a matrix "a", I need to evaluate trace(ax) as a function of > (matrix) "x". > > Here's a trace function: > > tr <- f

Re: [Rd] as.function()

2008-01-14 Thread Robin Hankin
On 14 Jan 2008, at 10:57, Prof Brian Ripley wrote: > On Mon, 14 Jan 2008, Henrique Dallazuanna wrote: > >> Try this: >> >> as.function.foo <- function(obj, ...) >> { >> newobj <- function(x, ...){} >> body(newobj) <- obj >> return(newobj) >> } >> >> x <- expression(2*x + 3*x^2) >> >> foo <- as.fu

Re: [Rd] as.function()

2008-01-14 Thread Prof Brian Ripley
On Mon, 14 Jan 2008, Henrique Dallazuanna wrote: > Try this: > > as.function.foo <- function(obj, ...) > { > newobj <- function(x, ...){} > body(newobj) <- obj > return(newobj) > } > > x <- expression(2*x + 3*x^2) > > foo <- as.function.foo(x) > foo(2) Well, that copies what as.function.polynomia

Re: [Rd] as.function()

2008-01-14 Thread Henrique Dallazuanna
Try this: as.function.foo <- function(obj, ...) { newobj <- function(x, ...){} body(newobj) <- obj return(newobj) } x <- expression(2*x + 3*x^2) foo <- as.function.foo(x) foo(2) Hope this help On 14/01/2008, Robin Hankin <[EMAIL PROTECTED]> wrote: > Antonio > > > thanks for your help here, bu

Re: [Rd] as.function()

2008-01-14 Thread Robin Hankin
Antonio thanks for your help here, but it doesn't answer my question. Perhaps if I outline my motivation it would help. I want to recreate the ability of the "polynom" package to do the following: > library(polynom) > p <- polynomial(1:4) > p 1 + 2*x + 3*x^2 + 4*x^3 > MySpecialFunction <

Re: [Rd] as.function()

2008-01-14 Thread Antonio, Fabio Di Narzo
2008/1/14, Robin Hankin <[EMAIL PROTECTED]>: > Hi > > [this after some considerable thought as to R-help vs R-devel] > > > > I want to write a (S3) method for as.function(); > toy example follows. > > Given a matrix "a", I need to evaluate trace(ax) as a function of > (matrix) "x". > > Here's a tra

[Rd] as.function()

2008-01-14 Thread Robin Hankin
Hi [this after some considerable thought as to R-help vs R-devel] I want to write a (S3) method for as.function(); toy example follows. Given a matrix "a", I need to evaluate trace(ax) as a function of (matrix) "x". Here's a trace function: tr <- function (a) { i <- seq_len(nrow(a))