• Stars
    star
    3
  • Rank 3,963,521 (Top 79 %)
  • Language
    R
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An alternative module system for R

Logo Modules for R

🚧 This package is deprecated! 🚧

This package is deprecated and has been replaced by the package ‘box’. ‘modules’ v0.9.x is in maintenance mode, and no new features will be implemented. ‘box’ is, for all intents and purposes, the spiritual successor: ‘modules’ v1.0.

Please refer to the migration guide for help upgrading from ‘modules’ v0.9.x to ‘box’.

Travis CI status Gitter

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.

More Repositories

1

box

Write reusable, composable and modular R code
R
737
star
2

named-operator

Named operators for C++
C++
487
star
3

cpp11-range

Range-based for loops to iterate over a range of numbers or values
C++
304
star
4

thesis

My PhD thesis, “Investigating the link between tRNA and mRNA abundance in mammals”
TeX
38
star
5

lisp.cpp

Minimal Lisp implementation in C++, inspired by “lispy”
C++
37
star
6

decorator

R function decorators
R
33
star
7

multifunction

A multicast function type for C++
C++
27
star
8

minimappr

Code minimaps for R
R
19
star
9

hyperlight

Automatically exported from code.google.com/p/hyperlight
PHP
16
star
10

fun

Module for functional programming in R
R
16
star
11

sys

Easily create reusable command line scripts with R
R
13
star
12

example-r-analysis

An example for an R analysis workflow using a Makefile, shell scripts and Knitr
R
12
star
13

trna

tRNA gene regulation downstream analysis
R
8
star
14

cv

Resume
TeX
6
star
15

unpack

Vector unpack assignment syntax for R
R
5
star
16

math-art

R
4
star
17

streampunk

Compiler for a pipe-based stream language to construct complex pipelines
C++
4
star
18

knitr-example

An example of a genomics analysis using knitr
4
star
19

rcane

Miscellaneous R tools that I haven’t had time yet to properly integrate. (deprecated and unmaintained)
R
3
star
20

parser-combinators

Parser combinators in R
R
2
star
21

system-setup

Bootstrap a usable system configuration for OS X
Shell
2
star
22

vim-snakemake

Snakemake Vim definitions, copied from https://bitbucket.org/snakemake/snakemake/
Vim Script
2
star
23

.files

My dotfiles repository
Shell
2
star
24

klmr.github.io

My website
JavaScript
2
star
25

switch-r

R version switcher for macOS
Shell
2
star
26

cpp11-raw-ptr

A raw pointer type for C++11
C++
2
star
27

te

Map, quantify and analyse transposable element expression
Makefile
2
star
28

ggplots

Some (different, better) defaults for ggplot2
R
2
star
29

trna-chip-pipeline

Upstream ChIP-seq analysis pipeline for tRNA data
Python
2
star
30

rnaseq-norm

Presentation slides about RNA-seq normalisation methods
Makefile
1
star
31

pichip

Makefile
1
star
32

codons

Analysis of adaptation of translation efficiency by means of codon–anticodon selection
R
1
star
33

bog-2015-poster

Biology of Genomes 2015 poster
PostScript
1
star
34

aka

R
1
star
35

roxydoxy

R
1
star
36

r-dict

R
1
star
37

poly-u

R
1
star
38

royal-jelly

R
1
star