52

I am working on a project which requires some R code to be run for some data analysis. The project is primarily in JavaScript, and I need a way to run R code from JS. My research has not found any good way to do it yet. Is there any way to do so?

Also, I have next to no experience with R (another person is supplying the R code).

6
  • Do you use IE or other browsers? Internet Explorer does have a mechanism to script Windows from javascript, but you would have to adjust your security settings ....then using Rscript.exe you can run yourScript.R like this var shell = new ActiveXObject('WScript.Shell');shell .Run("Rscript yourScript.R arg1");
    – agstudy
    Jul 15, 2013 at 23:28
  • Right now, I'm developing in Chrome. I don't think I will easily be able to switch.
    – H Khan
    Jul 15, 2013 at 23:29
  • This is Javascript on the browser right? This is for a web client? That's important, because these days JS isn't just a web client language, and can run as a fully-privileged language (see node.js) on a machine as well as a sandboxed language in a web browser.
    – Spacedman
    Jul 16, 2013 at 6:55
  • 1
    Do you already know about shiny? rstudio.com/shiny If you don't know about it, I would check it out before trying spending too much time doing things "the hard way". Especially notice the tutorial pages on using html directly, and the bit about dynamic ui; rstudio.github.io/shiny/tutorial/#dynamic-ui
    – geneorama
    Jul 16, 2013 at 14:29
  • @Spacedman Yes, although I may move the backend over to node.js in the future, it is currently a client-side browser-based project.
    – H Khan
    Jul 16, 2013 at 15:55

3 Answers 3

24

If you're ok with having the R code run on a server, then you should take a look at OpenCPU. It provides a REST API and corresponding JavaScript library for sending R code to the server and getting the results back. In particular, it takes care of the security problems that can arise from running R as a server (R code can run arbitrary shell commands, among other things). There are public demo instances that you can use to try it out, and this page provides a simple tutorial.

2
13

How about R-node ?

I think another alterative would be to use node.js as a server (http://nodejs.org/) and call R from within as a child process, search the Node.js API docs for specifics.

Also look at this for confirmation: Is it possible to execute an external program from within node.js?

Note: node can run any JS script(s) you may have, they don't necessarily need to be node-specific.

4
  • Could you add an explanation of R-node?
    – Jamie Bull
    Jul 16, 2013 at 0:07
  • 1
    Sure, I haven't personally tried it, but this is quoted directly from gitourious, you just needed to click that tiny more... link :) //// R-Node is a web front-end to the statistical analysis package R. Using this front-end, you can from any web browser connect to an R instance running on a remote (or local) server, and interact with it, sending commands and receiving the responses. In particular, graphing commands such as plot() and hist() will > execute in the browser, drawing the graph as an SVG image.
    – user2578094
    Jul 16, 2013 at 0:17
  • 2
    Always better to have the info here on SO, and even better in the answer itself. Web pages have a nasty habit of moving or being edited. Looks like a useful package though so hopefully it'll stick around.
    – Jamie Bull
    Jul 16, 2013 at 0:20
  • I think that this will probably be my best shot. Thanks!
    – H Khan
    Jul 17, 2013 at 19:49
2

This is by no means the best way, but I was able to do the following for my own Javascript+R project (silly.r is an R script that lives in the "r" directory). I basically ran the R code as a terminal command from my express server:

app.get('/sfunction', function (req, res) {
    exec('Rscript r/silly.r this is a test', function(error, stdout, stderr) {
        if (error) {
            console.log(error);
            res.send(error);
        }
        else if (stderr) {
            console.log(stderr);
            res.send(stderr);
        }
        else if (stdout) {
            console.log("RAN SUCCESSFULLY");
            res.sendfile("savedoutput/test.json");
        }
    });
});

The code is from lines 167-182 from here: https://github.com/ngopal/VisualEncodingEngine/blob/master/jsserver/app.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.