MyNixOS website logo
Description

Sources an R "Module" with Caching & Encapsulation, Returning Exported Vars.

In the fashion of 'node.js' <https://nodejs.org/>, requires a file, sourcing into the current environment only the variables explicitly specified in the module.exports or exports list variable. If the file was already sourced, the result of the earlier sourcing is returned to the caller.

lrequire for R

In the fashion of 'node.js' https://nodejs.org/, requires a file, sourcing into the current environment only the variables explicitly specified in the module.exports or exports list variable. If the file was already sourced, the result of the earlier sourcing is returned to the caller.

Installation

From CRAN:

install.packages("lrequire")

From GitHub:

devtools::install_github("rickwargo/lrequire")

Introduction

lrequire enables division of labor in R routines, only exposing variables that are necessary. lrequire-ing scripts keeps the enviornment clean and free of unused and unwanted variables. It also saves on execution time, as it caches the results from an earlier lrequire. The caching behaviour can be either suspended or it can re-source files that have been changed since the last cache of the file.

Example

Given the following unit file, named sample.R:

this = list(
  ten=      10,
  me=       "Rick",
  square=   function(x) { return (x*x) }
)

will.not.expose <- TRUE
this$power <- function(x, y) { return (x^y) }

module.exports = this

lrequire it and make use of it's outputs.

vals <- lrequire(sample)

print(paste("The square of 8 is ", vals$square(8)))

Upon lrequire-ing sample.R, only the this list will be exposed and assigned to the variable vals. It will have the following assignments:

  • vals$ten
  • vals$me
  • vals$square
  • vals$power

Note that vals$ten and vals$me are simple variables while both vals$square and vals$power are functions.

The next time the file is lrequire'd, the file will not be sourced - the cached value will be returned. To disable caching, pass do.caching = FALSE as a parameter to the lrequire() statement. Alternatively, set the variable module.change_code to 1 prior to the file being cached so any subsequent changes to the file, after it was cached, will cause it to be re-sourced.

Metadata

Version

0.1.3

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows