[R] String mathematical function to R-function

Liaw, Andy andy_liaw at merck.com
Sun Jul 9 03:13:33 CEST 2006


Here's one long-winded way of going about it, using R's ability to
manipulate functions as first class objects:
 
> f.string
[1] "x1 + sqrt(3*x2)"
> p.string <- parse(text=f.string)
> f <- function() {}        # empty function
> v <- all.vars(p.string)  # all the variables in the expression
> a <- do.call("alist", as.list(rep(TRUE, length(v)))) # contruct arguments
> names(a) <- v
> formals(f) <- a
> body(f) <- p.string
> f
function (x1 = TRUE, x2 = TRUE) 
x1 + sqrt(3 * x2)
> f(3, 4)
[1] 6.464102

Andy

  _____  

From: r-help-bounces at stat.math.ethz.ch on behalf of Renaud Lancelot
Sent: Sat 7/8/2006 6:25 PM
To: Cleber N.Borges
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] String mathematical function to R-function [Broadcast]



> fun_String <- "-100*x1 + 0*x2 + 100*x3" 
> 
> fun <- function(x1, x2, x3){ 
+   eval(parse(text = fun_String)) 
+   } 
> 
> fun(4:0, -2:2, 0:4) 
[1] -400 -200    0  200  400 

Best, 

Renaud 

2006/7/8, Cleber N.Borges <klebyn at yahoo.com.br>: 
> 
> 
> hello 
> 
> I make a subroutine that give-me a (mathematical) 
> function in string format. 
> I would like transform this string into function ( R 
> function ). 
> 
> thanks for any tips. 
> cleber 
> 
> 
> #e.g. 
> fun_String = "-100*x1 + 0*x2 + 100*x3" 
> 
> fun <- function(x1,x2,x3){ 
> return( 
> ############ 
> 
> evaluation( fun_String ) 
> 
> ############ 
> ) 
> 
> 
> 
> 
> 
> True String mathematical function :-(  :-( 
> 
> > nomes 
> [1] "8.49*x1*z1 + 6.13*x1*z2 + 6.4*x1*z3 + 6.9*x2*z1 + 
> 4.54*x2*z2 + 3.99*x2*z3 + 19.31*x3*z1 + 12.49*x3*z2 + 
> 3.86*x3*z3 + 5.25*x1*z1*z2 + -6.2*x1*z1*z3 + 
> 9.07*x1*z2*z3 + 10.87*x2*z1*z2 + 9.78*x2*z1*z3 + 
> 49.05*x2*z2*z3 + 4.56*x1*x2*z1 + -4.9*x1*x2*z2 + 
> 4.01*x1*x2*z3 + -0.39*x3*z1*z2 + 14.34*x3*z1*z3 + 
> 0.7*x3*z2*z3 + -2.84*x1*x3*z1 + 20.25*x1*x3*z2 + 
> 6.44*x1*x3*z3 + -4.91*x2*x3*z1 + 5.45*x2*x3*z2 + 
> 37.99*x2*x3*z3 + -22.24*x1*z1*z2*z3 + 
> -97.41*x2*z1*z2*z3 + -8.67*x1*x2*z1*z2 + 
> 49.14*x1*x2*z1*z3 + 14.24*x1*x2*z2*z3 + 
> 282.71*x3*z1*z2*z3 + 34.83*x1*x3*z1*z2 + 
> 111.2*x1*x3*z1*z3 + 101.38*x1*x3*z2*z3 + 
> -6.93*x2*x3*z1*z2 + 90.16*x2*x3*z1*z3 + 
> -9.11*x2*x3*z2*z3 + 17.22*x1*x2*x3*z1 + 
> -29.42*x1*x2*x3*z2 + -19.87*x1*x2*x3*z3 + 
> -277.41*x1*x2*z1*z2*z3 + -482.82*x1*x3*z1*z2*z3 + 
> -688.23*x2*x3*z1*z2*z3 + -588.4*x1*x2*x3*z1*z2 + 
> -197.31*x1*x2*x3*z1*z3 + -722.58*x1*x2*x3*z2*z3 + 
> 5536.59*x1*x2*x3*z1*z2*z3" 
> > 
> 
> 
> 
> 
> 
> _______________________________________________________ 
> 
> o discador agora! 
> 
> ______________________________________________ 
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>  
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
<http://www.R-project.org/posting-guide.html>  
> 


-- 
Renaud LANCELOT 
Département Elevage et Médecine Vétérinaire (EMVT) du CIRAD 
Directeur adjoint chargé des affaires scientifiques 

CIRAD, Animal Production and Veterinary Medicine Department 
Deputy director for scientific affairs 

Campus international de Baillarguet 
TA 30 / B (Bât. B, Bur. 214) 
34398 Montpellier Cedex 5 - France 
Tél   +33 (0)4 67 59 37 17 
Secr. +33 (0)4 67 59 39 04 
Fax   +33 (0)4 67 59 37 95 

______________________________________________ 
R-help at stat.math.ethz.ch mailing list 
https://stat.ethz.ch/mailman/listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>  
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
<http://www.R-project.org/posting-guide.html>



More information about the R-help mailing list