Last active
August 10, 2016 08:23
-
-
Save wviechtb/e8a23c13645a9d1ad413549e17eb1604 to your computer and use it in GitHub Desktop.
Function that either takes a variable from the data argument or from the parent frame.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <- function(x, data) { | |
if (missing(data)) | |
data <- NULL | |
if (is.null(data)) | |
data <- sys.frame(sys.parent()) | |
mf <- match.call() | |
mf.x <- mf[[match("x", names(mf))]] | |
x <- eval(mf.x, data, enclos=sys.frame(sys.parent())) | |
temp <- x^2 + 5 | |
return(temp) | |
} | |
dat <- data.frame(x=c(2,4)) | |
fun(x, dat) | |
fun(dat$x) | |
fun(c(2,4)) | |
fun(x) | |
x <- c(2,4) | |
fun(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment