有没有办法从 JavaScript 运行 R 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17665565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to run R code from JavaScript?
提问by H Khan
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?
我正在开发一个项目,该项目需要运行一些 R 代码以进行一些数据分析。该项目主要使用 JavaScript,我需要一种从 JS 运行 R 代码的方法。我的研究还没有找到任何好的方法来做到这一点。有什么办法吗?
Also, I have next to no experience with R (another person is supplying the R code).
另外,我几乎没有使用 R 的经验(另一个人正在提供 R 代码)。
采纳答案by Tom Panning
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 pageprovides a simple tutorial.
如果您同意在服务器上运行 R 代码,那么您应该看看OpenCPU。它提供了一个 REST API 和相应的 JavaScript 库,用于将 R 代码发送到服务器并返回结果。特别是,它处理了将 R 作为服务器运行时可能出现的安全问题(R 代码可以运行任意 shell 命令等)。您可以使用公共演示实例进行试用,此页面提供了一个简单的教程。
回答by Tom Panning
How about R-node?
如何R-节点?
I think another alterative would be to use node.jsas a server (http://nodejs.org/) and call R from within as a child process, search the Node.js API docs for specifics.
我认为另一种选择是使用node.js作为服务器 ( http://nodejs.org/) 并从内部调用 R 作为子进程,搜索 Node.js API 文档以获取详细信息。
Also look at this for confirmation: Is it possible to execute an external program from within node.js?
也看看这个确认:Is it possible to execute an external program from inside node.js?
Note: nodecan run any JS script(s) you may have, they don't necessarily need to be node-specific.
注意:node可以运行您可能拥有的任何 JS 脚本,它们不一定需要特定于节点。
回答by ngopal
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:
这绝不是最好的方法,但我能够为我自己的 Javascript+R 项目执行以下操作(silly.r 是一个位于“r”目录中的 R 脚本)。我基本上将 R 代码作为终端命令从我的快速服务器运行:
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
代码来自这里的第 167-182 行:https: //github.com/ngopal/VisualEncodingEngine/blob/master/jsserver/app.js