node-rscript

0.0.2 • Public • Published

node-rscript

Simple package for running R code from node, and passing large amounts of data back and forth.

Documentation

const nodeR = require("node-rscript")
 
/**
 * Uses R to run the script provided.
 * @param {String} script absolute path to the script you would like to run
 * @param {Object} userInput object that will be placed in the `input` variable in your script
 * @returns {Promise<Object>} 
 */
nodeR(script, userInput)

Example

This creates a chart in R from data passed in in node.

const nodeR = require("node-rscript");
const fsp = require("fs").promises;
 
const data = {
    graph: {
        x: [
            10,
            20,
            30,
            40
        ],
        y: [
            10,
            20,
            30,
            40
        ]
    }
};
 
 
fsp.mkdtemp("output")
    .then(tempdir => {
        return nodeR("test.R", { data, tempdir })
    })
    .then(output => {
 
 
        console.log(output);
 
        let file = fsp.readFile(output.filename[0]);
        console.log(file);
 
    });
library(jsonlite)
 
graph = input$graph
 
filename = tempfile(fileext = ".png", tmpdir=input$tempdir)
png(filename=filename)
result = plot(graph$x, graph$y,
main = "test", sub = "test sub",
xlab = "x", ylab = "y",
xlim = c(0, 100), ylim = c(0, 100))
# dev.off()
 
 # make the equivelent of a json object
output <- list(filename = filename)
 
jsonlite::toJSON(output)

Readme

Keywords

none

Package Sidebar

Install

npm i node-rscript

Weekly Downloads

0

Version

0.0.2

License

APACHE2

Unpacked Size

16.2 kB

Total Files

9

Last publish

Collaborators

  • macdja38