How to use Nodejs with R to get results - javascript

I am trying to use nodejs with R, how to Execute R algorithm via Node.Js and get the results.

Check out exec, a node module to run terminal statements.
Example:
var exec = require('exec')
exec('code to run my R script', function(err, response) {
if (err instanceof Error) throw err;
if (err) {
console.error('Something went wrong', err);
process.stderr.write(err);
}
// R output
console.log('All good', response);
process.stdout.write(response);
process.exit(1);
});
child_process is also a valid alternative. Checkout this: Run R script and display graph using node.js

Install OpenCPU && RStudio Server
Install node-opencpu client
Begin to use OpenCPU API

Related

How to install and use mongodb with node js?

I am very new to MongoDB and node.
So first of all:
How can I install MongoDB using the terminal on a Mac computer
Then how can I connect MongoDB with node?
How can I create a database in MongoDB using node?
Any help is appreciated. Thank You.
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:8080/mydb";
MongoClient.connect(url, function(err, db){
if(err)
{
console.log('error');
throw err;
}
else
{
console.log('success');
}
});
I tried this code but it shows an error.
This is the error I received:
You have to open the MongoDB server first.
Do this:
Go to the directory where mongo is installed: i.e.: <mongodb-install-directory>/bin
Start the server by : ./mongod --dbpath <mongo-data-directory>

Node.js - execFile throws spawn UNKNOWN

I am trying to spawn a service created with pyInstaller from an electron application. I am using the following code for that:
return new Promise((reject, resolve)=>{
var exec = require('child_process').execFile;
exec(path.join(install_path, 'myService.exe'), ['--startup=auto', 'install'], function(err, data) {
if(err) {
reject(err);
return;
}
console.log(data.toString());
exec(path.join(install_path, 'myService.exe'), ['start'], function(err, data){
if(err) {
reject(err);
return;
}
resolve(data.toString());
})
});
}
Unfortunately, this throws an
Uncaught Error: spawn UNKNOWN
on a testing system, which does not have node installed and is running Windows 10 x64. On my machine it is working fine.
Does anyone have tips how I could investigate this further? I am especially curious how this error is uncaught, because the callback functions obviously contain simple error handling.
Okay, after I built in better error handling thanks to Keiths help and rebuilt the project, the testers could not reproduce the issue anymore. I am still not sure if that actually fixed the problem or if the testers pulled an old version the last time.
Anyway, this is solved.

Meteor - Uncaught Error: Cannot find module 'mongodb'

I'm trying to establish a connection between my Meteor application and my MongoDB Atlas database.
I have the following bit of JavaScript:
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
MongoClient.connect('<MyMongoURL>', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
db.collection('largeTreeMap', function(err, docs) {
// Check for error
if(err) return console.log(err);
// Walk through the cursor
docs.find().each(function(err, doc) {
// Check for error
if(err) return console.err;
// Log document
console.log(doc);
})
});
}
db.close(); });
I added this to a blank JS document called test.js and upon running
node test.js
In my command line it returned the success message and data:
So now that I know the connection can be established I added the code to my Meteor project. I created a basic button and onClick the connection to MongoDB should completed.
However, instead I receive the following console error:
I understand from reading various Stack questions that this is a result of not running npm install mongodb in the project directory. However, I have tried doing this and the terminal returns:
Does any body know why the MongoDB is failing to install and preventing me from connecting to MongoDB in my application?
Any help would be much appreciated,
Many thanks,
G
You're trying to connect to the Mongo instance from the client, which is probably not what you want.
The mongodb npm package supports only Node.js, not JavaScript in the browser, as you can see from this line in its package.json
"engines": {
"node": ">=0.10.3"
},
In the case that worked, you are running it with Node.
What you probably want to do is to set the MONGO_URL environment variable to the Mongo Atlas instance, and leave the implementation of connecting / updating to Meteor itself.

Meteor how to run server side python script

I have a Meteor app that needs to call a python script to do some stuff in the background. How can I get this to work? I've tried using child_process and exec, but I can't seem to get it to execute properly. Where should the script even be located?
Thanks
I have same problems and its solved use python-sheel an npm packages to run Python scripts from Node.js
Install on meteor :
meteor npm install --save python-shell
There is simple usage :
var PythonShell = require('python-shell');
PythonShell.run('my_script.py', function (err) {
if (err) throw err;
console.log('finished');
});
If you want run with arguments and options :
var PythonShell = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'path/to/python',
pythonOptions: ['-u'],
scriptPath: 'path/to/my/scripts',
args: ['value1', 'value2', 'value3']
};
PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
here detail about python-shell https://github.com/extrabacon/python-shell
Thanks extrabacon :)

node.js: Talk to npm from within a module

I'd like to add a self-updating feature to a globally installed module. Is there a better way of doing it than this?
require("child_process").exec("npm update -g module-name");
There's some documentation about installing npm as a local dependency. Is this necessary? Is there any sample code on how to execute commands like update or install ?
Here's what I've usually done to use the system copy of npm instead of installing another copy of npm as a local module:
function loadNpm(cb) {
require('child_process').exec('npm', function(err, stdout, stderr) {
var m = /npm#[^ ]+ (.+)\n/i.exec(stdout);
if (!m)
return cb(new Error('Unable to find path in npm help message'));
cb(undefined, require(m[1]));
});
}
// usage ...
// only need to call `loadNpm()` once
loadNpm(function(err, npm) {
if (err) throw err;
// load() is required before using npm API
npm.load(function(err, npm) {
if (err) throw err;
// e.g. npm.search('ssh', true, function(err, results) { console.dir(results); });
});
});
Depending on your goals, here are a few options:
1) Via exec() as you mention. Don't forget to add an error callback.
2) Using the npm package as you mention.
For example, I wrote a quick script to install the Yeoman package globally which worked well. I didn't see a lot of documentation for this so I started reading the source code in the npm package itself.
var npm = require('npm');
npm.load (function (err, npm) {
if (err) {
console.log("Error loading");
return;
}
npm.config.set('global', true);
npm.commands.install(['yo'], function (err) {
if (err) {
console.error("Installation failed");
}
});
});
3) Another option is to just have a cron job auto-update packages if that is your goal.
4) You may also be interested in this package https://github.com/tjunnone/npm-check-updates

Categories