Hi, I am intending to save a path-describing character object in a slot of a class I'm working on. In order to have the option to use "system.file" etc in such string-saved path definitions, I wrote this
ExpressionEvaluator <- function(x){ x <- tryCatch( expr=base::parse(text=x), error = function(e){return(as.expression(x))}, finally=TRUE) return(x) } This produces > ExpressionEvaluator("system.file(\"INDEX\")") expression(system.file("INDEX")) > eval(ExpressionEvaluator("system.file(\"INDEX\")")) [1] "/usr/lib/R/library/base/INDEX" Which is what I want. However, > eval(ExpressionEvaluator("Test")) Error in eval(expr, envir, enclos) : object 'Test' not found prevents me from general usage (also in cases where "x" does NOT encode an expression). I don't understand why it is that > base::parse(text="Test") will return [1] expression(Test) while > as.expression("Test") produces [1] expression("Test") which would work with the eval call. Can anyone point out to me how to solve this generally? How can I feed the function a character object and get back an eval-able expression independent of whether there was an expression "encoded" in the input or not. Thank you for any hints. Sincerely, Joh ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.