Reputation: 843
I would like to ask if there is a way to see a variable hidden within a text.
if I run
k <- eval(expression(v <- 1))
then I get v equal to 1.
But how does it work if I have
k <- "v <- 1"
Thank you, in advance
Upvotes: 1
Views: 85
Reputation: 176648
Use parse(text=k)
to create an expression, then evaluate it:
eval(parse(text=k))
v
# [1] 1
Upvotes: 5
Reputation: 17517
Checkout the eval
and evalq
commands if you're wanting to evaluate the code.
If you just want to find any string before an <-
operator, I suppose you could use something like:
regexpr("(.*)[\\s]*<-", "a <- 1", perl=TRUE)
which would return the start index of the variable name, or -1 if there isn't one. You could extract it by using the substr
command, if you just wanted the variable name.
Upvotes: 0