Abdelali Amghar
Abdelali Amghar

Reputation: 63

Eval expression with a quoted variables

I have multiple expressions( func1, func2..) to evaluate with there parametres for each one :

eval(parse(text=func1), list(param1=exp1, param2=exp2, t =trunc),enclos=.GlobalEnv)

with :

func1 = "rho+lambda"; param1 = "rho"; param2 = "lambda"; exp1 = 0.1; exp2= 0.2 

indeed, it's work , but when a call the function that contain this eval, a error that variable "lambda" not found. it's a environnement problem when its a lists. i change the Enclos form parent.frame() to .GlobalEnv.

Any other solution ?!

Upvotes: 0

Views: 285

Answers (1)

Roland
Roland

Reputation: 132706

Usually the answer is "Don't use eval(parse())". There is almost always a much better alternative.

Anyway, you should check the list you pass to it:

func1 = "rho+lambda"; param1 = "rho"; param2 = "lambda"; exp1 = 0.1; exp2= 0.2 
eval(parse(text=func1), setNames(list(exp1, exp2), c(param1, param2)))
#[1] 0.3

Upvotes: 1

Related Questions