Use substitute:
> f <- function(e1, n) substitute(e1^n, list(e1 = e1, n = n[[1]]))
> f(expression(x^2+y^2), expression(n))
expression(x^2 + y^2)^n
On Fri, Sep 4, 2009 at 10:22 PM, Turner Rolf<r.turner at auckland.ac.nz>
wrote:>
> I am trying to construct a function to which I pass an expression as an
argument.
> >From that expression I want to create a somewhat more complicated
expression
> and then differentiate it using D() or deriv().
>
> To give a simple example, I'd like to be able to do something like
>
> e1 <- expression(x^2 + y^2)
> e2 <- some.magic(e1,"n")
>
> or maybe e2 <- some.magic(e1,expression(n))
>
> and get e2 being expression((x^2+y^2)^n). ?(Whence I can then do
D(e2,"x") and D(e2,"y").)
>
> Is there any way to accomplish this?
>
> Thanks.
>
> ? ? ? ?cheers,
>
> ? ? ? ? Rolf Turner
> ######################################################################
> Attention:
> This e-mail message is privileged and confidential. If you are not the
> intended recipient please delete the message and notify the sender.
> Any views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by MailMarshal
> www.marshalsoftware.com
> ######################################################################
>
> ______________________________________________
> R-help at r-project.org 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.
>