How to use eval Here is an example…

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)

Created by Pretty R at inside-R.org

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *