The sentence you quoted from the language definition is about standard evaluation, but replicate
uses non-standard evaluation. Here's its source:
replicate <- function (n, expr, simplify = "array")
sapply(integer(n), eval.parent(substitute(function(...) expr)),
simplify = simplify)
The substitute(function(...) expr)
call takes your expression x <- x + 1
without evaluating it, and creates a new function
function(...) x <- x + 1
That's the function that gets passed to sapply()
, which applies it to a vector of length n
. So all the assignments take place in the frame of that anonymous function.
When you use x <<- x + 1
, the evaluation still takes place in the constructed function, but its environment is the calling environment to replicate()
(because of the eval.parent
call), and that's where the assignment happens. That's why you get the increasing values in the output.
So I think you understood the manual correctly, but it didn't make clear it was talking there about the case of standard evaluation. The following paragraph hints at what's happening here:
It is possible to access the actual (not default) expressions used as arguments inside the function. The mechanism is implemented via promises. When a function is being evaluated the actual expression used as an argument is stored in the promise together with a pointer to the environment the function was called from. When (if) the argument is evaluated the stored expression is evaluated in the environment that the function was called from. Since only a pointer to the environment is used any changes made to that environment will be in effect during this evaluation. The resulting value is then also stored in a separate spot in the promise. Subsequent evaluations retrieve this stored value (a second evaluation is not carried out). Access to the unevaluated expression is also available using substitute.
but the help page for replicate()
doesn't make clear this is what it's doing.
BTW, your title asks about apply family functions: but most of them other than replicate
ask explicitly for a function, so this issue doesn't arise there. For example, it's obvious that this doesn't affect the global x
:
sapply(integer(10), function(i) x <- x + 1)