0.0.2 • Published 6 years ago

node-rscript v0.0.2

Weekly downloads
2
License
APACHE2
Repository
-
Last release
6 years ago

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)