How to seperate the one modules function across multiple .R files?

67 Views Asked by At

to organize the functions in my largest r-project, I am trying to migrate from a horribly flat folder structure to a nicely modularized hierarchical one using the box package. How can I keep on R-file per function in nested modules?

 R  
 |- ...  
 |- preproc  
 |     |- acti
...   ... |- __init__.R   <- How to export functions across all files   
          |- funA.R       <- stores funA()      
          |- funB.R       <- stores funB()  
          |- ...

After box::use(./R/preproc/acti) I would like to access all seperately stored functions via acti$funA() acti$funB() etc.

Many thanks!

In __init__.R, I tried

#' export
box:use(.)

which gives no access to the functions. And

#' export
box:use(./funA,./funB)

which gives overly nested calls: acti$funA$funA()

1

There are 1 best solutions below

1
On BEST ANSWER

‘box’ enforces “one file” = “one module”.

However, you can create a module which reexports the functions of its submodules (or anything else it imports). Your code already does that, but you are reexporting the modules themselves, not their exported names. — To export the names from inside the module, attach them inside preproc/acti/__init__.r:

#' @export
box::use(
  ./funA[...],
  ./funB[...],
)

Now you can use them directly:

box::use(./preproc/acti)

ls(acti)
# [1] "funA" "funB"

acti$funA()
# [1] "A"