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
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
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
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
>
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
>
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
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
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
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
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 <
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
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))
12 matches
Mail list logo