Skip to content

park-brian/r-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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()

About

A simple R wrapper for node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published