1.1.2 • Published 3 years ago

r-wrapper v1.1.2

Weekly downloads
4
License
ISC
Repository
github
Last release
3 years ago

r-wrapper

Prerequisites

  • Node.js 8+
  • R 3.0+
    • jsonlite

Installing

Ensure that R is in the current user's PATH.

# install jsonlite (note: Rscript.exe does not pick up all .libPaths() by default unless R_LIBS_USER is set, so the command below installs to the default .Library location)
Rscript -e "install.packages('jsonlite', repos='https://cloud.r-project.org/', lib = .Library)"

npm install --save r-wrapper

Example file: test.R

add <- function(a, b) {
    a + b
}

greet <- function(name, adjective) {
    paste('Hello', name, 'the', adjective)
}

Example: run functions from test.R

const r = require('r-wrapper');

// positional parameters are passed in through arrays
const sum = r('test.R', 'add', [2, 3]);
console.log(sum); // 5

// named parameters are passed in through objects
const greeting = r('test.R', 'greet', {
    name: 'John',
    adjective: 'Wise',
});
console.log(greeting); // "Hello John the Wise"

// child_process.execFileSync options can also be supplied
r('test.R', 'add', [1, 2], {
    /*
     * input: '...',
     * stdio: '...',
     * cwd: '...',
     * env: {...},
     * maxBuffer: x,
     * ...
     */
});

Example: run functions from test.R asynchronously

const r = require('r-wrapper').async;

// assuming we have top-level await support, or the following code is executed inside an async function

// positional parameters are passed in through arrays
const sum = await r('test.R', 'add', [2, 3]);
console.log(sum); // 5

// named parameters are passed in through objects
const greeting = await r('test.R', 'greet', {
    name: 'John',
    adjective: 'Wise',
});
console.log(greeting); // "Hello John the Wise"

// child_process.execFile options can also be supplied
await r('test.R', 'add', [1, 2], {
    /*
     * cwd: '...',
     * env: {...},
     * maxBuffer: x,
     * ...
     */
});

Notes

  • To prevent atomic vectors of length 1 from being unboxed, wrap them with I()
1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago