module: Define Modules in R

Description Usage Arguments Details Examples

View source: R/module.R

Description

Use module to define self contained organisational units. Modules have their own search path. import can be used to import packages. use can be used to import other modules. Use export to define which objects to export. expose can be used to reuse function definitions from another module.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
export(..., where = parent.frame())

expose(module, ..., reInit = TRUE, where = parent.frame())

extend(module, with)

import(from, ..., attach = TRUE, where = parent.frame())

module(expr = { }, topEncl = autoTopEncl(parent.frame()))

autoTopEncl(where)

use(module, ..., attach = FALSE, reInit = TRUE, where = parent.frame())

Arguments

...

(character, or unquoted expression) names to import from package or names to export from module. For exports a character of length 1 with a leading "^" is interpreted as regular expression.

where

(environment) typically the calling environment. Should only be relevant for testing.

module

(character | module) a module as file or folder name or a list representing a module.

reInit

(logical) whether to re-initialize module. This is only relevant if a module has state which can be changed. This argument is passed to as.module.

from

(character, or unquoted expression) a package name

attach

(logical) whether to attach the module to the search path

expr, with

an expression

topEncl

(environment) the root of the local search path. It is tried to find a good default via autoTopEncl.

Details

topEncl is the environment where the search of the module ends. autoTopEncl handles the different situations. In general it defaults to the base environment or the environment from which module has been called. If you are using use or expose referring to a module in a file, it will always be the base environment. When identical(topenv(parent.frame()), globalenv()) is false it (most likely) means that the module is part of a package. In that case the module defines a sub unit within a package but has access to the packages namespace. This is relevant when you use the function module explicitly. When you define a nested module the search path connects to the environment of the enclosing module.

import and use can replace library and attach. However they behave differently and are only designed to be used within modules. Both will work when called in the .GlobalEnv but should only be used for development and debugging of modules.

export will never export a function with a leading "." in its name.

expose is similar to use but instead of attaching a module it will copy all elements into the calling environment. This means that exposed functions can be (re-)exported.

extend can be used to extend an existing module definition. This feature is meant to be used by a module author to split a module definition into different files. You can also refer to a folder name in use which is interpreted to use all files as sub-modules.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## Not run: 
vignette("modulesInR", "modules")

## End(Not run)

m <- module({
  fun <- function(x) x
})

m$fun(1)

m <- module({

  import("stats", "median")
  export("fun")

  fun <- function(x) {
    ## This is an identity function
    ## x (ANY)
    x
  }

})

m$fun
m

modules documentation built on July 23, 2017, 1:02 a.m.