Inclusion of variables inside a loop

Therefore, consider the following piece of code that does not work, since most people can expect it

#cartoon example
a <- c(3,7,11)
f <- list()

#manual initialization
f[[1]]<-function(x) a[1]+x
f[[2]]<-function(x) a[2]+x
f[[3]]<-function(x) a[3]+x

#desired result for the rest of the examples
f[[1]](1)
# [1] 4
f[[3]](1)
# [1] 12

#attempted automation
for(i in 1:3) {
   f[[i]] <- function(x) a[i]+x
}

f[[1]](1)
# [1] 12
f[[3]](1)
# [1] 12

Note that we get 12 times after trying to "automate". Of course, the problem is that ifunctions are not contained in the private environment. All functions relate to the same thing iin the global environment (which can have only one value), since the for loop does not create another environment for each iteration.

sapply(f, environment)
# [[1]]
# <environment: R_GlobalEnv>
# [[2]]
# <environment: R_GlobalEnv>
# [[3]]
# <environment: R_GlobalEnv>

Therefore, although I could do with using local()and force()to fix the valuei

for(i in 1:3) {
   f[[i]] <- local({force(i); function(x) a[i]+x})
}

f[[1]](1)
# [1] 12
f[[3]](1)
# [1] 12

but it still does not work. I see that they all have different environments (through sapply(f, environment)), but they seem empty ( ls.str(envir=environment(f[[1]]))). Compare this to

for(i in 1:3) {
   f[[i]] <- local({ai<-i; function(x) a[ai]+x})
}

f[[1]](1)
# [1] 4
f[[3]](1)
# [1] 12

ls.str(envir=environment(f[[1]]))
# ai :  int 1
ls.str(envir=environment(f[[3]]))
# ai :  int 3

, force() , . , i . ,

#bad
f <- lapply(1:3, function(i) function(x) a[i]+x)
#good
f <- lapply(1:3, function(i) {force(i); function(x) a[i]+x})

i /, , for.

, : local() ? , force(), / ?

+4
3

, , , ( !).

for -loops, . ( , , force() - lapply()). , .

-, , lapply(), , :

a <- c(3,7,11)
f <- list()

## `for` loop equivalent of:
## f <- lapply(1:3, function(i) {force(i); function(x) a[i]+x})
for(i in 1:3) {
    f[[i]] <- {X <- function(i) {force(i); function(x) a[i]+x}; X(i)}
}
f[[1]](1)
# [1] 4

-, local(), ( ) i. , "" , . for -loop, , i, , , , .

a <- c(3,7,11)
f <- list()

for(i in 1:3) {
   f[[i]] <- local({i<-i; function(x) a[i]+x})
}
f[[1]](1)
# [1] 4
+4

?

ff<-list()
for(i in 1:3) {
    fillit <- parse(text=paste0('a[',i,']+x' ))
   ff[[i]] <- function(x) ''
   body(ff[[i]])[1]<-fillit
}

, " ".

+1

An alternative for force()which will work in a local for-loop environment is

capture <- function(...) {
    vars<-sapply(substitute(...()), deparse); 
    pf <- parent.frame(); 
    Map(assign, vars, mget(vars, envir=pf, inherits = TRUE), MoreArgs=list(envir=pf))
 }

which can then be used as

for(i in 1:3) {
    f[[i]] <- local({capture(i); function(x) a[i]+x})
}

(From the comments in Josh's comment above)

0
source

All Articles