Of course you have to be careful. Macros do not have their own environment.
A simple example is f (x, y) = (x + y) ^ 2:
m1 <- defmacro(x, y, expr = { x <- x + y x*x })
Executing m1 will change the input variables in the calling environment:
> x <- 1 > y <- 2 > m1(x, y) [1] 9 > x [1] 3 > y [1] 2
Normal R functions behave differently:
f1 <- function(x, y) { x <- x + y x*x } > x <- 1 > y <- 2 > f1(x, y) [1] 9 > x [1] 1 > y [1] 2
source share