Skip to content

Adding functions from separate scripts in a module #28

@wleoncio

Description

@wleoncio

Summary

  • How to properly add functions from different scripts into the same module?
  • How to call one of those functions from within another?

Details

I am currently developing an R package in which I'd like to group functions into modules. Each function is defined in its own .R file inside the R/ folder. To illustrate, say this is the basic structure of the package:

package
package/R
package/R/foo.R
package/R/foobar.R
package/R/modules.R
[...]

And the content of the files is as follows (plus Roxygen documentation):

foo.R

foo <- function() "foo"

bar.R

foobar <- function() paste(foo(), "bar") # notice it's dependency on foo()

modules.R

m <- module({
    export("foo", "foobar")
    expose("R/foo.R")
    expose("R/foobar.R")
})

I don't know if this is the proper way to declare m, since I understand expose() is expecting a script containing a function nested within a module, but that's what I did on a previous project and it worked fine.

The problem is that this time the fact that there's interdependence between my functions, I get scope errors I can't resolve.

What I've tried

  • Playing around with export(), import(), use() and expose()
  • Calling foo() either as above or as m$foo()

What I'm avoiding

I imagine that I could bunch up foo() and foobar() under one R file and maybe wrap them around a module and that would solve the issue, but in my real case I have more than a dozen long functions on each module, so putting all functions together could very well get me to debugging and versiol control hell.

Intended usage

FWIW, this is how I intend users to use the functions within the package:

m$foo()
m$foobar()
?foo
?foobar
?m

Although it would be even better if one could load a module and then use its functions, i.e.:

load(m) # import()? use()?
foo()
foobar()

I don't know if the modules package does this, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions