R scope: force variable substitution in function without local environment
问题 I'm defining functions in a loop and trying to force evaluation of a loop variable without having to carry around a private environment. Example: a set of functions handlers$h1 , handlers$h2 , ..., handlers$h6 that just pass through the 1, 2, ..., 6 to another function like so: handlers <- list() for (i in 1:6) { handlers[[paste0('h', i)]] <- function () { message(i) # <-- example } } So handlers$h1() should message 1, handlers$h2() should message 2, ... Instead, all of the functions return 6