Skip to content
Prev 250599 / 398498 Next

Defining functions inside loops

Hello again.

Let me try something a little more intricate. Let's say instead of
forcing evaluation of 'i' I'd want to force evaluation of a vector;
for example:
s <- c( 0.2, 0.45, 0.38, 0.9)
f  <- lapply(1:10, function(i)local({ force(i) ; function(x)x^2+s[i]}))
rm(s)
f[[1]](0.1)
Error in f[[1]](0.1) : object 's' not found

Any thoughts?

Best regards,

Eduardo
R version 2.11.1 (2010-05-31)
x86_64-pc-mingw32

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252
[3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Brazil.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] Revobase_4.2.0   RevoScaleR_1.1-1 lattice_0.19-13

loaded via a namespace (and not attached):
[1] grid_2.11.1       pkgXMLBuilder_1.0 revoIpe_1.0       tools_2.11.1
[5] XML_3.1-0