Discussion:
[nodejs] Calling R script from nodejs using r-script
Amrita Ray
2016-09-05 17:32:28 UTC
Permalink
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?
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/82f128cc-c479-4615-8cb4-3617090c24ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Aria Stewart
2016-09-05 19:44:51 UTC
Permalink
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 \\: <smb://:> 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
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/33E1BD20-904D-4C00-B293-C19A30068201%40dinhe.net.
For more options, visit https://groups.google.com/d/optout.
Jeff Fong
2016-09-29 17:21:56 UTC
Permalink
@ Rayamr, did you ever figure this out? I have the same problem.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/a9500554-6b5e-4de3-8378-c7c45d62363a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
CESAR BASURTO
2016-10-21 20:17:51 UTC
Permalink
I have the same problem.
Post by Amrita Ray
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?
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/523ca235-228a-4e54-9619-2c029234d3b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Mahdi Ben Amar' via nodejs
2016-11-19 12:16:58 UTC
Permalink
I am facing the same problem. Does anybody found a solution ?
Post by Amrita Ray
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?
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/97c3b82b-8cf9-42aa-aff4-6ab56f1a3b4e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pine Fe
2017-02-08 22:12:44 UTC
Permalink
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.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2882cab1-a072-43b4-bade-8e2e066bc209%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nate Gardner
2017-07-20 00:49:02 UTC
Permalink
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.
Post by Pine Fe
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
"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.
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/947435f0-34da-4b30-872f-22d840d05c5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Srinivas Gurubilli
2018-05-28 06:19:28 UTC
Permalink
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....
--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/841e5a35-926e-41c0-b367-9a5507edd3e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...