[Rd] Passing R code from webpage

Simon Urbanek simon.urbanek at r-project.org
Mon Feb 18 17:02:18 CET 2013


On Feb 18, 2013, at 10:20 AM, Matevz Pavlic wrote:

> Hi, 
> 
> 
>> Not at all - R has a built-in webserver (it's used for the help pages), so
> if you install R, you're done with that part. Rook >gives you a wrapper for
> that. 
> 
> What do you mean by wrapper? 
> 

It defines an API that is easier to use that writing the httpd function (which is called by the build-in HTTP server) yourself.


>> The problem is it doesn't scale, so if you're happy with one-user solution
>> then you can use R without anything. If you 
>> need something that scales, then you need something else -- and for
>> Windows you're a bit out of luck, because the lack > of fork+COW on
>> Windows (BTW cygwin doesn't help there, either) paired with the fact that
>> R is not thread-safe means i
>> quite hard to get scalability on Windows involving R. The best bet on
>> Windows are server solutions that keep a pool of 
>> individual R instances as workers but I'm not aware of any off the top of
>> my head (I recall some Java solutions way back
>> when Java was hip and I have it on my ToDo list for Rserve but it's not
>> there yet). 
> 
> What do you mean by scale/scaling? (not an IT engineer). Mainly this would
> be used for one-user-at-the-time. So if by scaling you mean multiple user at
> the same time,

Yes

> this would not be a problem....
> 

Ok, you're fine then.


>> I have look in all packages i can imaggine, Rook, ggoleVis, Shiny....but
>> in 
>> all i get the problem when i want to connect to a MS SQL database. 
>> 
> 
>> That shouldn't really matter - as long as you can connect from any R
>> session, you will be able to connect through R from 
>> the webserver. 
> 
> The problem is that i don't even know how to start getting data from SQL and
> put it to R and get plot back ;)
> 

Why would you want to pull the data client-side? That is quite cumbersome since you have to rely on the client (browser) to be able to connect to the database -- I don't think that's easy. It's much easier to just tell R what to query for and let R pull the data, plot it and return the plots and results.


>> This is the workflow that i'd like to achieve : 
>> 1.) in webrowser connect to MS SQL database 
> 
>> I assume you mean from the R script? Otherwise you'll have to shove the
>> data across (not impossible but why not 
>> connect from R?).  
> 
> Yes, i know how to do it from R script. Don't know how to do this in HTML.
> 
> I just need some pointers (or even better example of code) on how to use R
> script in HTML.
> 

I'm not sure what you really mean ;) - using R from HTML simply means sending R code from the client (browser) to the server and have the server run that code. That's easy - you simply issue a request, for example
http://myserver/R?eval=print(1:10)
It is inherently unsafe (anyone can do anything on your server), but in principle possible. In the real world you probably don't want to do that (unless you don't care about security) - you want to have the R script on the server to define what to do and just accept input, which could be a SQL query for example (still some security implications but not as bad) or even better just arguments that define what to look up.

For example (this is using FastRWeb API but it should illustrate the idea):

foo.R:

run <- function(city, ...) {
  if (missing(city)) return("Sorry, you have to specify a city!")
  db <- dbConnect("myDatabase:...")
  q <- dbSendQuery(db, "SELECT age FROM user WHERE city=?", city)
  age <- fetch(q, n = -1)[[1]]
  dbClearResult(q)
  dbDisconnect(db)
  p <- WebPlot(400, 400)
  hist(age)
  p
}
 
In HTML you can either use a form

<form action=R/foo>
City: <input type=text name=city>
<input type=submit>
</form>

or AJAX into a div
 
<a href=# onclick='javascript:req('foo?city=Paris', 'result');'>Paris</a><p>
<div id=result></div>

where the req() JS function could look like this (a more complete one is in the FastRWeb examples):

function req(what, where) {
  if (!window.ajax) {
   if (window.XMLHttpRequest)
    window.ajax = new XMLHttpRequest();
  else if (window.ActiveXObject)
    window.ajax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  var ajax= window.ajax;
  ajax.open("GET",what);
  ajax.onreadystatechange = function() {
  if (ajax.readyState == 4) {
    if (ajax.status == 200)
      document.getElementById(where).innerHTML = ajax.responseText;
    else
      document.getElementById(where).innerHTML = "<b>Sorry, cannot load this page</b> ("+ajax.status+" "+window.ajax.statusText+")";
    }
  }
  ajax.send(null);
  return false;
}

Either way, you get the idea - you could pass a SQL query instead or use eval() in the argument if you want (as I said, that's too insecure for my taste). But in all cases you are really running everything server-side.


That said, there is potentially another solution -- I don't know anything about CSHTML, but if it can really hook into C# from HTML then you make be able to use the C# part to connect to R - there is a C# Rserve client (assuming that CSHTML is server-side interpretation). This is theoretical, I don't use Windows so I don't know if that is meant to work.

Cheers,
Simon


> 
> Thanks, m
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Passing-R-code-from-webpage-tp4658800p4658944.html
> Sent from the R devel mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list