Takes a character
/text
string and evaluates it as if it was code (an R expression). parse()
returns the text as if it was code. eval
then runs/evaluates the code afterwards.
evalparse(x)
x | A |
---|
Simplistically, the purpose of this idea is to get into the realm of code that can write code, but this is dependent on either the input parameters or the data, so not automatic. The purpose of this specific function, however, is for saving on typing: eval(parse(text = x))
VS evalparse(x)
.
eval
, parse
if(runif(n = 1) >= 0.5){ vec <- c("mickey mouse", "bambi", "herbert hoover") } else { vec <- c(12.6548, 13.21549, 84.946562) } my_string <- sprintf( "as.%s( substring( trimws(vec, which = 'left') , first = 1 , last = 4 ) )" , class(vec) ) evalparse(my_string)#> [1] 12.6 13.2 84.9