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 researc
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