You can probably eval what you’ve got there, but is it absolutely necessary to be working from a string? In general, if you’re trying to do something with a string that involves calling some function depending on the contents of the string, you’re not going to be able to do it without eval somewhere, so you’re probably better off doing something like
@eval(@formula($(Meta.parse(text))))
(much as I hate to say it
)
What’s going on here is that Meta.parse is converting your string into a Julia Expr, the $(...) is inserting that into the expression starting with @formula, and then @eval is evaluating the whole thing. It’s basically as if you’d typed @formula y ~ x + z into the REPL.
It’s generally a dangerous idea to use @eval in scripts since it can lead to performance gotchas unless you’re VERY careful, but in this case I don’t see a way around it. The usual advice we give to people trying to construct a formula on the fly is to wrap their term symbols in Terms and combine them with +, &, and ~, but if you have to be able to handle ANY formula that’s valid in R that won’t work (short of writing your own parser basically).