Skip to content

Instantly share code, notes, and snippets.

@hadley
Created July 6, 2012 23:44
Show Gist options
  • Save hadley/3063436 to your computer and use it in GitHub Desktop.
Save hadley/3063436 to your computer and use it in GitHub Desktop.
with(x, {
f(z)
})
# Which of the following statements is this equivalent to?
#
# a) x$f(x$z)
# b) f(x$z)
# c) x$f(z)
# d) f(z)
#
# Scroll down for the answer
# It depends on what's in x!
x <- list()
f <- function(x) x + 1
z <- 1
with(x, {
f(z)
})
x <- list(x = 2)
with(x, {
f(z)
})
x <- list(f = function(x) x + 10)
with(x, {
f(z)
})
x <- list(f = function(x) x + 10, z = 10)
with(x, {
f(z)
})
# This is one reason not to use with - you need to know
# exactly what's in x before you can reason about the
# result of the code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment