简体   繁体   中英

Passing character as variable name in R

I am trying to solve an implicit equation in R using multiroot function from package rootSolve .

I am reading the implicit equation from a text file using parse . Also, the variable to be solved for is read from a text file as a character.

For using multiroot ,

multiroot(function, initial_guess, ....))

we have to generate a function from the read equation. I did this by

fun <- function(op) {fun <- eval(expr.im)}
op = as.name(opim.names)

where expr.im is the read implicit equation as an expression from the text file, and opim.names is the variable to be solved for, as character.

But the problem arises when I pass the variable op to be solved as a symbol to the function. It gives an error saying that the object

"variable to be solved for" not found.

I think that the variable symbol is not being passed correctly in the function.

Please tell me how to do it correctly.

Since a lot of stuff is going on in my code, I cannot post the whole thing here. Let me just state a small example for it.

var.name = "x1" # This is what I read from the text file #
var.sym = as.name(var.name)

func <- function(var.sym){
func = x1^2      # the expression x1^2 is also read from a text file  #
}                  # I am trying to solve the implicit equation x1^2 = 0  #
initial_guess = 1
root = multiroot(f=func, start = initial_guess)

As requested by nicola here's what I want - I have a text file giving me the name of the variable and its initial guess. I read the variable name (say "x") and the initial guess value (say 1) into variables var (character) and guess (numeric). I also have another text file containing the following equation -

x^3-1

I read this as an expression in the variable expr .

I want to find the solution to the implicit equation expr . (The text files can have different names of variables and correspondingly an implicit expression in another file)

As you know, for using the multiroot function, we need to have a function. The problem is I am not able to pass the variable name stored in var to the function.

Any further clarification will be given if asked.

You can build your function in the following way.

#input: function expression, variable names and initial guess
expr<-"x^3-1"
var.name<-"x"
initial.guess<-2
#we build an "empty" function
func<-function() {}
#now we set the formal arguments and the body of the function
formals(func)<-eval(parse(text=paste0("alist(",paste(var.name,collapse="=,"),"=)")))
body(func)<-parse(text=expr)
#we can see that func is correctly defined
func
#function (x) 
#x^3 - 1
#now we can call multiroot
multiroot(func,initial.guess)
#$root
#[1] 1
#$f.root
#[1] 3.733019e-08
#$iter
#[1] 6
#$estim.precis
#[1] 3.733019e-08

You need a little more care if you are dealing with function of more than one variable. See ?multiroot to check how to pass the arguments. Basically you have to pass just one argument which is a vector of all the function arguments. It shouldn't be difficult if you take some time to see how I managed to build func . If you are exclusively dealing with one variable function, you should use the base uniroot function.

Not able to understand the description fully. But to answer the heading, you can try this procedure-

a = "random_string"
b = "a"
eval(parse(text = b))
[1] "random_string"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM