I'm using Renjin to evaluate R formulas from my Java program. Basically the user is typing an R formula in a text field, and I want R/Renjin to do the parsing for me, and return a list of the variables in that expression. Say if the user inputs a*b, I want ["a", "b"]
as the result.
I looked at the RParser.parse()
method on the Java part. But when I call then RParser.getResult()
I get a SEXP
object from which I didn't find a way to get the variables names.
On the R side, I looked at parse(text="a*b")
. I can eval()
it, but I didn't find a way to get the names either.
Any ideas?
EDIT
From R, it's possible to achieve this with:
> d = getParseData(parse(text="a*b"))
> d$text[which(d$token=="SYMBOL")]
[1] "a" "b"
The problem is, when I run it with Renjin, I get Exception in thread "main" org.renjin.eval.EvalException: getParseData() is not currently supported by Renjin
.
Still looking…
The functions all.vars
and all.names
will return all the names in a formula. The difference is that all.names
also includes function names, while all.vars
excludes them.
> f <- y ~ a + sin(b)
> f
y ~ a + sin(b)
> all.vars(f)
[1] "y" "a" "b"
> all.names(f)
[1] "~" "y" "+" "a" "sin" "b"
I don't know if Renjin supports these, but they're fairly basic.