I have an existing Javascript/HTML Application. I wanted to use power of R Programming's scientific computing and graphics.
My aim is to,
Send some data from Javascript app.
Call predefined R functions with the data input.
Get the output get the output in the form of both text and graphics.
Display it in the HTML page.
How to achieve this,
Should I run R continuously, use something like web sockets and connect to R? If doing How to pass R scripts to execute and get the output pack?
There is Rserve. There are some nodeJS implementation for Rserve. But problem with this is, each line of code should be passed through the evaluate commands. Even though if I do so, how to handle the graph output?
I explored a bit of openCPU. If using openCPU R package, R should be continuously Running with opencpu library and each we start R and openCPU, it starts with different port number. And if i close the R session, opencpu server also terminates.
If I install standalone opencpu server in my machine, how to use R with this? I've installed openCPU standalone server and a kind of stuck after that.
How should I proceed, What should I do to accomplish my task. I'm like a kind of don't know which direction to go. Please throw some light on this. I'm sure most people would need this.
I have worked with shiny, but in this case, I can not make use of it. Need to connect R from external Web Application.
FastRWeb sounds like it would be perfect for your needs. From the documentation:
FastRWeb is an infrastructure that allows any webserver to use R
scripts for generating content on the fly, such as web pages or
graphics. URLs are mapped to scripts and can have optional arguments
that are passed to the R function run from the script. For example
http://my.server/cgi-bin/R/foo.png?n=100 would cause FastRWeb to look
up a script foo.png.R, source it and call run(n="100"). So for example
the script could be as simple as
run <- function(n=10, ...) {
p <- WebPlot(800, 600)
n <- as.integer(n)
plot(rnorm(n), rnorm(n), col=2, pch=19)
p
}
This can potentially then be called using JavaScript to dynamically load images and display them.
You might also like to think about shiny, though that's more of a complete solution.
You can call R from javascript efficiently using Rserve package. There is javascript implementation of Rserve client available rserve-js.
Additionally you may find it interesting the httupv implementation of R as a service described in this Suggestions needed for building R server REST API's that I can call from external app?.
Related
In a nutshell, I want to write some code that will take in a URL and return a dataframe with this page's HTML elements and their CSS values. Now my major problem is finding a way to make JS and R communicate.
At this point, I need to mention that I have a limited coding knowledge (i'm a marketer). I learn R and need to use it to complete the task.
I've found that "getComputedStyle" is the JS function I need, but the page must be open in the browser to return the actual computed styles.
Thus, I've come up with a solution below:
Creating some simple Chrome plugin that would send the current site's URL to R, R would create a list of XML node paths and in a loop, using querySelector and getComputedStyle, populate a dataframe with the CSS values returned by the plugin.
However, I don't know how to make JS and R communicate.
I need to make it work for me only so should I somehow run R on localhost (I use Mac OS)? And then what?
I've read a little about OpenCPU but am not sure if that's what I need.
Or maybe there's some other solution?
I am working on a project that requires a gsm Module (sim900d) send two location coordinates (current location and destination) to a server and get the direction using google maps api in response.
I started out with JavaScript and php and latter realized that JavaScript is client side and php is server side and that I can't send a javascript variable to php. I now know that I'll have to use node.js for my project. I have a little experience with JavaScript but my node.js knowledge is limited to hello world web server.
I've tried running directions-test.js for node-googlemaps unsuccessfully.
The exact code was giving an error "cannot find module '../lib/googlemaps'" so i replaced the "'../lib/googlemaps'" with 'googlemaps', it runs fine but all i get is sqrt(OK) >> 2 honored (0.896s).
I know I have to insert my google maps api key somewhere but can't figure out where and the syntax. And can someone please tell me where the directions object would be?
Edit: I am an electrical engineering student, please be elaborate in your answers. My project is EE related, I cannot learn everything and write code from scratch (due to time constraints) so related example codes would be much appreciated.
Edit 2: I have just realized that directions-test.js for node-googlemaps is not an example code, rather its Test code using vows. Vows as its website defines it is was built from the ground up to test asynchronous code. It executes your tests in parallel when it makes sense, and sequentially when there are dependencies.
var gm = require('googlemaps');
var util = require('util');
gm.config('key', '<insert your api key here>');
gm.directions('31.470656,74.412929', '31.470789,74.408619' ,
function(err, data){util.puts(JSON.stringify(data));});
This is how you get directions between two locations.
I am trying to connect monetdb with node.js. I have a simple (20 line) c program which can query moentdb using mapi libraries.
Can I use those libraries to build something(module/addon) for node.js which uses these libraries and connect to monetdb?
(using odbc is an option but it have its own disadvantages.)
Update1 :
node-ffi is pretty awesome. I was able to create a fetch table program pretty easily. (I have added my working code for example.)
So if I have 3 options
1. ODBC
2. node-ffi
3. a c program to fetch database data and listens to connection from node.js through socket
In terms of performance which is better option to implement, if I have little less time to develop a addon for node.js
var ffi = require("ffi");
var libmylibrary = ffi.Library('/usr/local/lib/libmapi.so', {
"mapi_connect":["int",["string",'int',"string","string","string","string"]],
"mapi_query":['int',["int","string"]],
"mapi_fetch_row":["int",["int"]],
"mapi_fetch_field":["string",["int","int"]]
});
var res = libmylibrary.mapi_connect("localhost", 50000,"monetdb", "monetdb", "sql", "demo");
console.log(res);
var ret=libmylibrary.mapi_query(res,"select * from table");
while(libmylibrary.mapi_fetch_row(ret)){
console.log(libmylibrary.mapi_fetch_field(ret,0));
console.log(libmylibrary.mapi_fetch_field(ret,1));
}
Update 2:
Above code is not recommended for production use...it does not use async functionality of node.js so please use it for baby steps
While FFI makes it easy to call native code, you really shouldn't use it to for something you have to do often, like calling a database library. From the docs:
There is non-trivial overhead associated with FFI calls. Comparing a hard-coded binding version of strtoul() to an FFI version of strtoul() shows that the native hard-coded binding is orders of magnitude faster. So don't just use the C version of a function just because it's faster. There's a significant cost in FFI calls, so make them worth it.
In other words, FFI works, but is slow. This is fine if you just need to make a few calls, but very bad news if you need to make frequent calls.
What you need to do is write an addon. Addons are C++ modules that provide glue to C and C++ libraries. (Just because you must write the addon in C++ doesn't mean you can't call pure C code from the addon!)
The node docs provide plenty of examples that should get you started. If you're working with Windows, here's some tips to get VS set up.
If the calls to the C library are blocking, you'll need to make them async. libuv provides a thread pool you can do the work on.
I'm trying to run envjs and Rhino in a java application to render SVGs with D3.js.
So far, I can evaluate smaller functions using Rhino but when it comes to setting up envjs, the problems begin. The most important one is that the only tutorial for envjs talks about a file called env.rhino.js. But I have no idea where to find it.
Can anybody help me out?
(Yes, google shows some results but they are not officially belonging to Rhino or envjs)
I know this answer is very late. But I want to do the same and had the same problems - maybe this will help the next one. It took a while to figure out which one of the hundrets of github forks on env-js will do the job. I found out that this combination will work for a simple test:
git clone https://github.com/thatcher/envjs-site.git
#note the different fork!
wget https://raw.github.com/thatcher/env-js/master/src/dom/sizzle.js
wget http://d3js.org/d3.v3.min.js
java -jar dist/env-js-1.1.jar
load("lib/env.rhino.js");
load("sizzle.js");
load("d3.v3.min.js");
d3.select("body").append("svg").selectAll("line").data([1,2]).enter().append("line").attr("x1", function(d){return d;});
document.innerHTML;
<html><head/><body><line/><line/><svg xmlns="http://www.w3.org/2000/svg"><line x1="1"/><line x1="2"/></svg></body><line/><line/></html>
j
First, download env.rhino.js.
Then, use this Java code to start a Rhino instance and load Env.js:
import org.mozilla.javascript.Context;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.Main;
Context cx = Context.enter();
Global scope = new Global(cx);
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_5);
Now you can load and run a JavaScript file (using its absolute file-system path)
Main.processFile(cx, scope, ABSOLUTE_PATH_TO_SOME_JAVASCRIPT_FILE);
And/or evaluate JavaScript code and get its String result
(String)cx.evaluateString(scope, "alert('Its WORKING!')", "js", 1, null);
I did attempt this but couldn't go very far. I also wanted to generate SVGs on server side, with requests being initiated from java(Glassfish in my case). The only way you can do this is with jsdom & Node.js. I am able to do this successfully. Sadly, other than Node.js + jsdom, there seems no other way to do this.
Once you get it working, there are bigger problems lurking if you try to heavily load Node.js with SVG generation requests.
Recently I have developed a short Python script using the Flask framework to start a process over HTTP (see here). Now there is the question for an adequate technology to "pipe" the standard streams (I am especially interested in stdout and stderr) of started processes to the Web.
What would you say is the most suitable way to accomplish this? First I thought of websockets to fit perfectly. But then it seemed to me that most implementations veil their connected clients. I think that I need this information in order to be able to send the output to all of them.
Edit: I think that my question was a bit unclear: I want to see the output of the executed command in a web-interface. So I have to "convey" data from stdout/stderr of the executed process via HTTP somehow to the web-interface(s). It might be possible that the started command may run for some (longer) time. The example which invokes the dd command is not representative as it does not output anything in the given setting. And sure, I would have to use the output of the subprocess.Process class and objects (e. g. by using the communicate() method).
Not very clear what your goal is. Your code has this. Perhaps that's a clue?
current_app.process = subprocess.Popen(
['dd', 'if=/dev/zero', 'of=/dev/null'])
Trawl through the docs for subprocess. You'll find you can specify stdout and stderr.
http://docs.python.org/library/subprocess.html#module-subprocess
One strategy might be to capture stdout/err to file then rewrite that file to your http response.
One approach that will work (and is non-blocking, can serve multiple clients) is using Twisted:
Connect a ProcessProtocol
http://twistedmatrix.com/documents/current/core/howto/process.html
to a Twisted WebSocket server
https://github.com/tavendo/AutobahnPython
Disclosure: I am author of Autobahn.