Giter Club home page Giter Club logo

js-call-r's Introduction

js-call-r

JavaScript call R function with a simple way.

Integrate JaveScript and R.
JavaScript passes JSON to R script, and get JSON from R script by js-call-r module.

Install

npm install --save js-call-r

Usage

const rscript = require('js-call-r');

// Sync
rscript.callSync(RScript, [Args], [Options]);

// Async, support Promise while callback is undefined
rscript.call(RScript, [Args], [Options]);
rscript.call(Rscript, [Args], [Options], [callback]);

Synchronous

const result = call.callSync('./test/test.R', {
    a: 1,
    b: 3
});
console.log(result.result);

Asynchronous

call.call('./test/test.R', {
    a: 1,
    b: 3
})
.then((result) => {
    console.log(result.result);
})
.catch(err => {
    console.log('err = ', err);
});

Or

call.call('./test/test.R', {
    a: 1,
    b: 3
}, undefined, (result) => {
    console.log(result.result);
});

Options

{
    "verboseResult": true/[false]
}

false is default.

Result

If verboseResult of Options is true, call() or callSync() will return the following object.

{
    "pid": <number>,
    "result": <return from R script>
}

If it is false, the result will only include <return from R script> part.

While any error occurs, the result will be the following object.

{
    "pid": <number>,
    "error": <error message>
}

R script template

Transcate JSON data between JavaScript and R, so input and output for R scripts have to convert to JSON format.

# json library
library('rjson')

# function to call
sumFunc <- function (num1, num2){
  sum(num1, num2)
}

# get arguments of cli
args <- commandArgs(trailingOnly = TRUE)

# arguments to JSON
json <- fromJSON(args)

# call function
ret <- sumFunc(as.numeric(json$a),as.numeric(json$b))

# convert return of function to list
output <- list(result = ret)

# output JSON
print(toJSON(output));

Example

npm test

License

No License

js-call-r's People

Contributors

codejie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

js-call-r's Issues

Cannot read property 'toString' of null

Tried to install and run the test, but get this error. (Suspect it is an environment error rather than the code, but can't work out what!)

node test/test.js

call R script to calc(Sync) "1 + 3 = "
D:\Dev\play\index.js:123
error: result.stdout.toString()
^

TypeError: Cannot read property 'toString' of null
at Object.callSync (D:\Dev\play\index.js:123:34)
at Object. (D:\Dev\play\test\test.js:7:21)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:282:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
npm ERR! Test failed. See above for more details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.