Calling R script from nodejs using r-script

3,777 views
Skip to first unread message

Amrita Ray

unread,
Sep 5, 2016, 3:19:30 PM9/5/16
to nodejs
I am trying to call some R scripts from nodejs. I found out from the net that a simple npm module called r-script is all I need to do this. I installed it and then wrote this simple program.

var R = require("r-script");
var out = R("C:\Users\IBM_ADMIN\Documents\Amrita-Documents\BAT Pricing Analytics\a-test.R")
          .data(10)
          .callSync();
         
console.log(out);

The R script a-test.R looks like this.

Square <- function(input) {
  return(input^2)
}

res <-Square(3)
print(res)

I have tried this dozens of times but getting the same output.

C:\Users\IBM_ADMIN\Documents\Amrita-Documents\NodeJS\My Sample Codes>node r-code
.js
null

Can anyone please help?

Aria Stewart

unread,
Sep 5, 2016, 3:50:27 PM9/5/16
to nod...@googlegroups.com

On Sep 5, 2016, at 13:32, Amrita Ray <ray...@gmail.com> wrote:

I am trying to call some R scripts from nodejs. I found out from the net that a simple npm module called r-script is all I need to do this. I installed it and then wrote this simple program.

var R = require("r-script");
var out = R("C:\Users\IBM_ADMIN\Documents\Amrita-Documents\BAT Pricing Analytics\a-test.R")
          .data(10)
          .callSync();
          

My suspicion is twofold: one is that the r-script module isn't reporting errors well or at all
two is that you need to make those \ into \\: these are javascript strings, so \ is an escape character, and so to get one into the string, you have to double them. Otherwise they're replaced as the js is loaded either with nothing (if they're nonsense) or with special characters (if it happens to be something like \t, you get a tab.)

Aria

Jeff Fong

unread,
Oct 3, 2016, 9:08:05 AM10/3/16
to nodejs
@ Rayamr, did you ever figure this out?  I have the same problem.


CESAR BASURTO

unread,
Oct 21, 2016, 4:18:49 PM10/21/16
to nodejs
 I have the same problem.

Mahdi Ben Amar

unread,
Nov 20, 2016, 10:04:58 AM11/20/16
to nodejs
I am facing the same problem. Does anybody found a solution ? 

Pine Fe

unread,
Feb 11, 2017, 1:30:35 AM2/11/17
to nodejs
For Windows users (maybe for Linux too):

You need to add the environment variable of R executables to the windows %PATH%.

1. Open the windows File Explorer
2. Right click on "My computer" or "This computer"
3. Click on properties
4. Click on advanced system configuration (something like that).
5. A windows pops-up
6. Click on environment variables
7. Click (highlight) the PATH variable
8. Click Edit...
9. Then add the path to the folder where your Rcmd, R.exe and Rscript.exe files are....

My path is: C:\Program Files\R\R-3.3.2\bin\x64

Because "Program Files" has a white space, the environment variable has to be added with double quotes, like:

"C:\Program Files\R\R-3.3.2\bin\x64" or else it won't work.

Then NodeJS's r-script package will be able to call the R command from any folder.

You can check if the environment variable was added by opening your CMD and entering "R" and then enter. If it says that the command is not recognized, then the variable wasn't correctly added. If it was correctly added, you will enter R console mode.

Close all CMD's and execute again your NodeJS app.

Nate Gardner

unread,
Jul 20, 2017, 2:31:22 PM7/20/17
to nodejs
This is the solution! Also, you must make sure your R_LIBS_USER is set correctly (in .Renviron) and verify that your packages are installed in the user library folder.

Srinivas Gurubilli

unread,
May 28, 2018, 2:33:10 AM5/28/18
to nodejs
I am getting the below error, while running R script from nodejs:

        .data("hello world", 20)
        ^

TypeError: Cannot read property 'data' of undefined
    at Object.<anonymous> (C:\Users\srinig\RScripting\test1.js:5:9)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

Can some one please help me in calling r script by using node js....

Diogo Santos

unread,
Jun 4, 2018, 5:26:22 PM6/4/18
to nodejs
Change your init function, in your index.js (in the r-script folder) to this:

function init(path) {
var obj = new R(path);
_.bindAll(obj, "data", "call", "callSync");
return obj;
}

krishnap...@gmail.com

unread,
Apr 19, 2019, 1:07:30 PM4/19/19
to nodejs


On Saturday, February 11, 2017 at 12:00:35 PM UTC+5:30, Pine Fe wrote:
For Windows users (maybe for Linux too):

After adding path in environment variables... in cmd i am getting error like 'R' is not recognized as an internal or external command,
operable program or batch file.

What is the solution for this?? 
Reply all
Reply to author
Forward
0 new messages