I am relatively new to R and I would like to do the following: if I write a simple function as
fn<-function(K) K^2
and then compute fn(10)
, I get 100.
Now if K^2
is created somewhere as a string, t1="K^2"
, then obviously my function does not work anymore since it does not take K as a variable.
How can I turn a string, a sequence of characters into a line in my function?
I don't want to use eval(parse(text=t1))
, because I would like to use my function later in another function, say to find the gradient using n1.grad(x0,fn)
.
Thanks, Yasin