I’m currently calling rp.slider from the tkrplot library with multiple arguments in a loop, for example:
rp.slider(rpplot, param1)
rp.slider(rpplot, param2)
etc.
Ideally, I’d like to do this within a loop, e.g.
for(i in 1:10)
rp.slider(rpplot, foo(paste(param,i,sep="")))
Where foo will encode the string to a variable name (symbol?). rp.slider converts the argument into a string using deparse(substitute(var)). Is there a foo function that will let me do this? I’ve tried as.symbol, as.name, and parse (among others) without success.
Any help would be much appreciated!
To clarify, deparse(substitute(x)) returns [1] "x" – I’d like a way of returning the same output from a string, i.e. which foo outputs [1] "x" for input deparse(substitute(foo("x")))? Is it possible?
Try
eval(parse(text=...))oreval(substitute(...)).parse(text=...)turns the string in an expression,evalevaluates the expression. Be sure to use thetextargument, asparsenormally looks for a file. Forgetting that is a common mistake. See also?parseand?eval.To show how to use it, your adjusted code :
substitutesubstitutes values in a language object by the strings given in the second argument :Or, using the example in the help files :
The explanation why this is necessary, can be found within the source code of rp.slider. The construct to get the varname inside the function is not the standard used in R. In fact, the use of ‘deparse(substitute())’ is strongly discouraged, exactly for this reason. With most functions,
as.expression("x")works to get the variable in using a variable name. Alas, the author of therpanelpackage made this impossible.