foreach icon indicating copy to clipboard operation
foreach copied to clipboard

Passing .options causes eager evaluation of all arguments in foreach()

Open mikmart opened this issue 2 years ago • 0 comments

Passing any .options causes all arguments to foreach() to be evaluated eagerly in a context where that may not be appropriate:

library(foreach)
foreach(i = 1:3) %:% foreach(j = i:3, .options.foo = "bar") %do% seq(i, j) |> str()
#> Error in eval(expr, envir, enclos): object 'i' not found
# Expected result
foreach(i = 1:3) %:% foreach(j = i:3) %do% seq(i, j) |> str()
#> List of 3
#>  $ :List of 3
#>   ..$ : int 1
#>   ..$ : int [1:2] 1 2
#>   ..$ : int [1:3] 1 2 3
#>  $ :List of 2
#>   ..$ : int 2
#>   ..$ : int [1:2] 2 3
#>  $ :List of 1
#>   ..$ : int 3

Discovered via this StackOverflow question: https://stackoverflow.com/q/76291120/4550695

mikmart avatar May 19 '23 19:05 mikmart