Same DeployR enabled R Script, diffrent client.
This example can be used in conjunction with the mlds-ui-demo to demonstrate how a different DeployR consumer/client can invoke the same R Script /sheri/geo/RUGclusters.R and do something different with the result. For this example, the geoJSON returned from calling /sheri/geo/RUGclusters.R is stored in CouchDB rather than building a UI as we did in mlds-ui-demo.
- Install Node.js
- Install CouchDB
- Create a new Database named
feature
from the CouchDB Admin Console $ git clone https://github.com/mlds-deployr/mlds-couchdb-demo.git
$ cd mlds-couchdb-demo
$ npm install
$ node index.js
This runs the demo.- Open the CouchDB Admin Console and
view the newly inserted geoJSON returned from executing the R Script
/sheri/geo/RUGclusters.R
under the
feature
Database.
var deployr = require('deployr').configure({ host: 'http://166.78.105.110:7400' }),
stream = require('stream')
JSONStream = require('JSONStream'),
got = require('got'),
nano = require('nano')('http://localhost:5984'),
feature = nano.db.use('feature');
//
// Stream Transform duplex to download DeployR response artifact URLs in
// geoJSON and store it in CouchDB
//
var geo = new stream.Transform({ objectMode: true });
geo._transform = function(chunk, encoding, done) {
var data = chunk.toString();
if (data) {
got.get(data.replace(/\"/g, ''))
.pipe(feature.insert(null, 'feature'));
}
done();
};
//
// Execute `RUGclusters.R` in DeployR and pipe geoJSON response into CouchDB
//
deployr.script('/sheri/geo/RUGclusters.R')
.numeric('nclus', 5)
.pipe(JSONStream.parse('deployr.response.execution.artifacts.*.url'))
.pipe(JSONStream.stringify(false))
.pipe(geo);