klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
862 stars 48 forks source link

Make box `future`-proof #314

Closed mlell closed 1 year ago

mlell commented 1 year ago

Error description

The future package implements promise objects and flexible switching between parallel worker setups independent of analysis code.

It currently is not usable together with imports of the form box::use(file[...]) because it registers something on the search path that future cannot read because it expects "normal" packages there.

If I write a file box/test.R with content:

testfun <- function(x){
  message("Hello ",x)
}

I get an error with this:

library(future)
box::use(box/test[testfun])
f <- future(testfun("world"))
value(f)
## Error in env$.packageName : name '.packageName' not found in 'env'

I want to file a bug report to "future" but I do not know a constructive suggestion because I am not familiar with the internals of "box".

At the end, future needs to read properties from "box" environments that are sufficient to load those modules on the worker.

I know that I can extract information using pos.to.env() and attributes():

e2 <- pos.to.env(2)
str(attributes(e2))
## List of 5
##  $ name     : chr "mod:box/test"
##  $ class    : chr "box$mod"
##  $ spec     :List of 5
##   ..$ name    : chr "test"
##   ..$ prefix  : chr "box"
##   ..$ attach  : Named chr "testfun"
##   .. ..- attr(*, "names")= chr "testfun"
##   ..$ alias   : chr "test"
##   ..$ explicit: logi FALSE
##   ..- attr(*, "class")= chr [1:2] "box$mod_spec" "box$spec"
##  $ info     :List of 2
##   ..$ name       : chr "test"
##   ..$ source_path: chr "/proj/src/box/test.R"
##   ..- attr(*, "class")= chr [1:2] "box$mod_info" "box$info"
##  $ namespace:Class 'box$ns' <environment: 0x55675f648258> 
##   ..- attr(*, "name")= chr "namespace:test"
##   ..- attr(*, "loading")= logi FALSE

R version

```plain text > R.version platform x86_64-pc-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 4 minor 0.4 year 2021 month 02 day 15 svn rev 80002 language R version.string R version 4.0.4 (2021-02-15) nickname Lost Library Book ```

‘box’ version

1.1.0

klmr commented 1 year ago

This appears to be a duplicate of #285 and should be fixed when updating the ‘globals’ package that ‘future’ depends on.

mlell commented 1 year ago

Ah, yes I can confirm that it works with globals 0.16.2. Should have remembered that my analysis environment is not up to date... thank you for the hint.