Learn R Programming

⚠️There's a newer version (0.13.0) of this package.Take me there.

Modules for R

Table of contents

Summary

This package provides an alternative mechanism of organising reusable code into units, called “modules”. Its usage and organisation is reminiscent of Python’s. It is designed so that normal R source files are automatically modules, and need not be augmented by meta information or wrapped in order to be distributed, in contrast to R packages.

Modules are loaded via the syntax

module = import('module')

Where module is the name of a module. Like in Python, modules can be grouped together in submodules, so that a name of a module could be, e.g. tools/strings. This could be used via

str = import('tools/strings')

This will import the code from a file with the name tools/strings.r, located either under the local directory or at a predefined, configurable location.

Exported functions of the module could then be accessed via str$func:

some_string = 'Hello, World!'
upper = str$to_upper(some_string)
# => 'HELLO, WORLD!'

Notice that we’ve aliased the actual module name to str in user code.

Alternatively, modules can be imported into the global namespace:

import('tools/strings', attach = TRUE)

The module is then added to R’s search() vector (or equivalent) so that functions can be accessed without qualifying the module’s imported name each time.

R modules are normal R source files. However, import is different from source in some crucial regards. It’s also crucially different from normal packages. Please refer to the comparison for details.

But I need packages!

Not to worry, simply use import_package instead of import and treat the imported package the same way you would treat a module:

dplyr = import_package('dplyr')
cars %>% dplyr$filter(speed > 15)

For consistency, library and require should not be used in conjunction with modules (although they can).

Installation

To install using devtools, just type the following command in R:

devtools::install_github('klmr/modules')

Wiki: Installation has more information.

Usage basics

Local, single-file modules can be used as-is: assuming you have a file called foo.r in your current directory, execute

foo = import('foo')
# or: foo = import('./foo')

in R to make its content accessible via a module, and use it via foo$function_name(…). Alternatively, you can use

import('foo', attach = TRUE)

But this form is usually discouraged (at the file scope) since it clutters the global search path (although it’s worth noting that modules are isolated namespaced and don’t leak their scope).

If you want to access a module in a non-local path, the cleanest way is to create a central repository (e.g. at ~/.R/modules) and to copy module source files there. Now import needs to know how to find this repository. This can be done by either setting the environment variable R_IMPORT_PATH or, inside R (e.g. in ~/.Rprofile), via options('import.path').

Nested modules (called “packages” in Python, but for obvious reasons this name is not used for R modules) are directories (either local, or in the import search path) which optionally contain an __init__.r file. Assuming you have such a module foo, inside which is a submodule bar, you can then make it available in R via

foo = import('foo')     # Make available foo, or
bar = import('foo/bar') # Make available only foo/bar

During module development, you can reload a module to reflect its changes inside R, or unload it. In order to do this, you need to have assigned the result of import to an identifier.

Copy Link

Version

Install

install.packages('modules')

Monthly Downloads

2,696

Version

0.9.8

License

Apache License (== 2.0) | file LICENSE

Maintainer

Konrad Rudolph

Last Published

January 21st, 2024

Functions in modules (0.9.8)

export_submodule_

Export a given submodule from the current module
find_module

Find a module<U+2019>s source code location
is_S3_user_generic

Check whether a function given by name is a user-defined generic
export_operators

Copy a module<U+2019>s operators into a separate environment
exhibit_namespace

Exhibit objects from an internal module namespace as an environment
loaded_modules

Environment of loaded modules
module_name

Get a module<U+2019>s name
module_path

Get a module<U+2019>s path
reload

Reload a given module
script_path

Return an R script<U+2019>s path
do_import

Perform the actual import operation on an individual module
module_base_path

Get a module<U+2019>s base directory
attach_module

Attach a module environment locally or globally
module_attributes

Module attributes
split_path

Split a path into its components and merge them back together
set_script_path

Set the base path of the script.
module_file

Find the full file names of files in modules
module_init_files

Return a list of paths to a module<U+2019>s __init__.r files
fix_module_attributes

Save a module<U+2019>s inherited attributes into a local environment
make_S3_methods_known

Find and register S3 methods inside a module
module_help

Display module documentation
lsf

Return a list of functions in an environment
register_S3_method

Register an S3 method for a given generic and class.
modules

An alternative module system for R
unload

Unload a given module
import_

Import a module into the current scope
import_search_path

Return the import module search path