6

I have an existing Javascript/HTML Application. I wanted to use power of R Programming's scientific computing and graphics.

My aim is to,

  1. Send some data from Javascript app.
  2. Call predefined R functions with the data input.
  3. Get the output get the output in the form of both text and graphics.
  4. Display it in the HTML page.

How to achieve this,

  1. 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?

  2. 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?

  3. 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.

  4. 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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Manoj G
  • 1,776
  • 2
  • 24
  • 29
  • Another option would be to export the data into a portable format (like csv) and run Rscript on an `.R` file that will find the data. – Roman Luštrik Jul 02 '15 at 16:59
  • [rapier](http://trestletech.com/2015/06/rapier-convert-r-code-to-a-web-api/) might be an option for you – hrbrmstr Jul 02 '15 at 17:01
  • @RomanLuštrik: But how to get the graphics out and display in the app on the fly? – Manoj G Jul 02 '15 at 17:06
  • @hrbrmstr: Thanks for your suggestion. I'll explore this. – Manoj G Jul 02 '15 at 17:07
  • You can save the figures as files. There may be better, more elegant options, though. – Roman Luštrik Jul 02 '15 at 17:38
  • 1
    Where do you get stuck with the opencpu server? You just install it on a linux box and then you can start calling functions in your R packages via the API. – Jeroen Ooms Jul 03 '15 at 09:52
  • @Jeroen: Thanks Jeroen. OpenCPU is great! I got stuck up because of some other dependent library. Basic test passed! Now trying to get it work with my app. Going through example apps. It solves all my problem if I get it done. :) – Manoj G Jul 08 '15 at 08:39
  • @ManojG Hello sir, did you figure out how to resolve the issue? – Hamid May 19 '18 at 03:08

2 Answers2

2

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.

Community
  • 1
  • 1
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
  • 1
    Thank you. I'll try this out. I'm aware of `shiny`. I love it. I've worked with it. But in this case, I can not make use of shiny. – Manoj G Jul 03 '15 at 04:25
0

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?.

Community
  • 1
  • 1
jangorecki
  • 16,384
  • 4
  • 79
  • 160