Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up an R based service on a web page [closed]

Tags:

r

web

I would like to provide the following service on a webpage:

  • Running an R-script daily which collects publicly available data and does some calculations (I have developed the script already, it needs some additional libs!)
  • Posting the updated graphical and numerical/textual output on the webpage

I don't want to run my own computer all the time so a kind of a cloud solution should be employed (I guess?!?).

Do you have any ideas how to accomplish the above ideas?

like image 651
vonjd Avatar asked Feb 07 '12 09:02

vonjd


2 Answers

You may have a look at FastRWeb - it serves R scripts as if they were web pages and supports graphics as well as regular output. It uses Rserve which makes it much faster than any solution that involves starting R (such as R or Rscript). It works on any webserver wither via CGI or PHP.

A script to generate a plot would look like:

run <- function(n=100, ...) {
   p <- WebPlot(800, 600)
   n <- as.integer(n)
   plot(rnorm(n), rnorm(n), col=2, pch=19)
   p
}

Other solution is RApache which embeds R directly into the apache webserver.

Edit: And also by Jeff there is now Rook that uses the embedded R HTTP server (thanks to Joshua for pointing that one out).

You mentioned running it daily - if you don't need online analysis, you can simply generate html pages and png files with R and send them to your webserver - all in an automated script. There are many R package that facilitate HTML output - just search in the CRAN package list for HTML.

like image 106
Simon Urbanek Avatar answered Oct 23 '22 11:10

Simon Urbanek


Use Rscript and cat to print an HTTP response like you would from any CGI-bin. For example, set the content type of the response and then cat some HTML, or print a PNG, etc:

#!/path/to/Rscript

cat("Content-type: text/html\n\n")
cat("<html>")
cat("<body>")
cat("<p><em>Hello</em>, world!</p>")
v <- round(runif(10)*10, 0)  # sample ten random integers from {0..10}
cat("<p>", v, "</p>")
cat("</body>")
cat("</html>")
like image 5
Alex Reynolds Avatar answered Oct 23 '22 09:10

Alex Reynolds