Skip to content

Instantly share code, notes, and snippets.

@wviechtb
Last active August 10, 2016 08:23
Show Gist options
  • Save wviechtb/e8a23c13645a9d1ad413549e17eb1604 to your computer and use it in GitHub Desktop.
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.
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