mlr-org / mlr3misc

Miscellaneous helper functions for mlr3
https://mlr3misc.mlr-org.com
GNU Lesser General Public License v3.0
11 stars 2 forks source link

Bug in crate function #87

Closed sebffischer closed 7 months ago

sebffischer commented 1 year ago
library(mlr3misc)

l = list(a = 1)

crate(function() print(a), a = l$a)
#> Error in vapply(.x, .f, FUN.VALUE = .value, USE.NAMES = FALSE, ...): values must be length 1,
#>  but FUN(X[[2]]) result is length 3

Created on 2023-08-27 with reprex v2.0.2

sebffischer commented 11 months ago

Ok this is no bug I think but at least something that can be improved

mb706 commented 7 months ago

The way crate() is set up currently is that it only passes along variables from the surrounding environment to the crated environment. The benefit here is that the static code check done by R CMD check will not have any problems with this.

If crate() allowed what you are doing in your code, the code checker would admonish that the function() is referencing a non-existent variable a. crate() forcing you to write it as follows is therefore a good thing:

library(mlr3misc)

l = list(a = 1)

a = l$a
crate(function() print(a), a)
sebffischer commented 7 months ago

thx!