npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

r-integration

v2.4.0

Published

Simple portable library used to interact with pre-installed R compiler by running commands or scripts(files)

Downloads

2,247

Readme

R-integration - Node JS API

Node JS CI

This is the R-integration API which allows you to execute arbitrary R commands or scripts directly from the node JS environment. This integration works on Windows and GNU/Linux based systems and uses system calls to access the R binary.

Installation

Node JS

npm install r-integration

Usage

Run a simple command

You can run a simple R command by using the executeRCommand(command) function which takes a String as parameter (the command to execute) and returns a String Array containing the results

// In NodeJS
const R = require('r-integration');

let result = R.executeRCommand("max(1,2,3)");
console.log(result);

> [ '3' ]

Additionally if you want to execute multiple commands you can pass them, comma separated, in the command parameter

const R = require('r-integration');

let result = R.executeRCommand("max(1,2,3); min(1,2,3); exp(2)*pi");
console.log(result);

> [ '3', '1', '23.2134' ]

Run an external script

If you want to run a complex and external script, instead of passing all of the comma separated commands to the executeRCommand(command) function, you can use the executeRScript(file_location) function, which takes a String file_location as parameter(the location where the file is stored) and returns a String Array containing all the results of the R script execution.

NOTE: in order to read all the results you have to explicitly use the print() function in R to send a variable/data to the result Array in Node JS.

Example

Suppose we have a R script located in ./scripts/test.R

# In R
x <- 5
y <- 6
z <- 3
result <- max(x, y, z)

print(result)

As you can see we exposed the result variable with the print() function in order to read it from the Node JS environment. Now we can compile the R script and finally read that variable from Node JS

// In NodeJS
const R = require('r-integration');

let result = R.executeRScript("./scripts/test.R");
console.log(result);

> [ '6' ]

Call a R function with parameters

If you want to execute a R function in an external file you can use the callMethod function by passing the fileLocation, methodName (function to call) and params (the params passed to methodName at call). Note that params must be an Object in the format {variableName1: "value", variableName2: "value", .. }. You can also pass an array as an argument to a function. NOTE that the resulting array will be converted into an R array of the format c(...).

Example

Suppose we have a R script located in ./scripts/test.R

x = function(data) {
    return(data * 2)
}

Now from the NodeJS environment we can call the x function

// In NodeJS
const R = require('r-integration');

let result = R.callMethod("./scripts/test.R", "x", {data: "2"});
console.log(result);

> [ '4' ]

Call a standard R function with parameters

If you want to execute a standard R function you can use the callStandardMethod function by passing the the methodName (function to call) and params (the params passed to methodName at call). Note that params must be an Object in the format {variableName1: "value", variableName2: "value", .. }.

Example

Suppose you want to call the standard library max function From the NodeJS environment we can call the max function in the following way

// In NodeJS
const R = require('r-integration');

let result = R.callStandardMethod("max", ["2","3","4"]);
console.log(result);

> [ '4' ]

NOTE: in the above example an array is passed to the function max.

Async calls

If you need to execute asynchronously the functions executeRCommand and callMethod you have to use executeRCommandAsync and callMethodAsyncby using promises

Example

Suppose we have a R script located in ./scripts/test.R

x = function(data) {
    return(data * 2)
}

Then in Node JS you can use promises in the following way

// In NodeJS
const R = require('r-integration');

callMethodAsync("./scripts/test.R", "x", {data: "2"}).then((result) => {
    console.log(result);
}).catch((error) => {
    console.error(error);
})

> [ '4' ]

R scripts syntax rules

In order to read the R script output from the Node JS environment you can use 2 methods

  1. use the R print(...) function
  2. use the R cat(...) function

It's recommended to use print() instead of cat(). IMPORTANT: If you use cat() remember to put the newline character \n at the end of each cat call. In the previous example we used print(). This was equal to use cat() in the following way

# file ./scripts/test.R
x <- 5
y <- 6
z <- 3
result <- max(x, y, z)

cat(result, sep="\n")

Alternative R binaries location

If you installed R in a not standard location you can provide an additional parameter RBinariesLocation to all the functions mentioned above to specify the alternative location.

Example

Suppose we have a R installed in the directory C:\Program Files\R and we want to execute a R command by using the binaries installed in the mentioned directory. The solution is to execute the executeRCommand(command, RBinariesLocation) by passing the correct binaries location.

// In NodeJS
const R = require('r-integration');

let result = R.executeRCommand("max(1,2,3)", "C:\\Program Files\\R");
console.log(result);

> [ '3' ]

Building Requirements

Windows / Mac OS

For Windows and Mac OS users refer to the links above.

Ubuntu - Debian

sudo apt-get install r-base
sudo apt-get install nodejs
sudo apt-get install npm

Arch Linux

sudo pacman -S r
sudo pacman -S nodejs
sudo pacman -S npm