How to use eval()?
Here is an example.
test.dframe <- data.frame(x = 1:10, y = rnorm(10)) addSmooth <- function(data, smooth = TRUE) { require(ggplot2) p <- "ggplot(data = data, aes(x = x, y = y))" p.line <- "geom_line()" p.smooth <- "stat_smooth()" if (smooth == TRUE) { p.all <- paste(p, p.line, p.smooth, sep = " + ") } else { p.all <- paste(p, p.line, sep = " + ") }. eval(parse(text = p.all)) } addSmooth(test.dframe, smooth = FALSE)
Leave a Reply