From shane.conway at gmail.com  Thu Oct 21 16:33:35 2010
From: shane.conway at gmail.com (Shane Conway)
Date: Thu, 21 Oct 2010 10:33:35 -0400
Subject: [Rd] Scripting SVG with R
In-Reply-To: <AANLkTinLPOG0W5XXjr60iKw4xWrRxRW-P21i5qkOtBac@mail.gmail.com>
References: <4CB5D0A8.5040901@embl.de>
	<AANLkTinLPOG0W5XXjr60iKw4xWrRxRW-P21i5qkOtBac@mail.gmail.com>
Message-ID: <AANLkTinogrxfTLHghk8NFVDBxKzSQTrNX5c_6xRE2=BJ@mail.gmail.com>

This is slightly tangential, but I would just add some notes related
to things that I had worked on (in various stages of completion).
This relates more to parsing SVG and Javascript from within R, rather
than R from within Javascript:

1) I wrapped most of the Protovis primitives in an R package called
webvis (http://cran.r-project.org/web/packages/webvis/).  If you're
not familiar, Protovis is an excellent low-level plotting package in
Javascript (http://vis.stanford.edu/protovis/) out of Jeffrey Heer's
lab at Stanford (http://vis.stanford.edu/).  Protovis produces SVG
graphics as the output.  As a first pass, I decided to just use the
web browser to parse the javascript.  You can see how this works by
calling demo("playfairs.wheat") or looking at the plot.webvis help
examples.

2) My goal was to have the next version of webvis parse the javascript
directly in R.  To that end, I started working on another package that
would wrap the javax.script.* library using rJava.  This works for me,
and it allows you to pass code in any scripting language that runs on
the JVM (including Jython, Jaskell, Clojure, and Javascript).  The
problem is that you need more than javascript parsing in order to use
something like Protovis: you need the complete web browser context.
You can get this by using env-js (http://www.envjs.com/).  But that
leads to another problem: the version of the javascript parser (called
Rhino) in javax.script wasn't the latest, so I would need to use Rhino
directly (you can see part of my discussion that here:
http://stackoverflow.com/questions/2664578/using-javax-script-or-rhino-to-run-javascript-in-java-with-browser-context-e-g).
 That's where I stopped.

What would the end result of webvis be so far as it pertains to your
problem?  With a small amount of effort, it would be pretty straight
forward to create interactive graphics that are pure javascript from
R.  I had envisioned that this would be more useful for an interactive
R session, but I know of one project (RNode) that's currently using it
from a browser through Rserve (see
http://squirelove.net/r-node/doku.php).

Hope that this is useful.

Regards,
Shane


On Fri, Oct 15, 2010 at 11:06 AM, Gabriel Becker <gmbecker at ucdavis.edu> wrote:
>
> Wolfgang,
>
> I am currently working on this very thing under Duncan Temple Lang. Or, to
> be more precise what you are describing is one piece of our overarching
> goal, which is to create a system in which R can be run from within a
> browser (currently FireFox) with bi-directional communication occurring
> between the javascript/svg/flash engines and R. This will allow us to, for
> example, build web pages which can contain both R code (which can be
> executed in-browser) and "live"graphics devices in the form of
> javascript/svg/flash canvases, as per your query. It will also, eg, allow
> for the building of rich, easily customizable GUIs for R through the
> (relatively painless though not easy to do well) process of designing
> interactive/multimedia web pages.
>
> Duncan has done quite a bit of work of this nature in the past, even going
> so far as embedding R in the Netscape browser some 10 years ago
> (SNetscape<http://www.omegahat.org/SNetscape/>note this is quite
> outdated now). More recently, last summer he created a
> package (RFirefox, not currently available, see below) which embeds R within
> Firefox (3.5) but currently only allows for communication from javascript
> down into R (so R code can be evaluated and the results passed back up to
> the javascript engine), before handing the project off to me to work on
> under his supervision. Currently the installation/configuration is _very_
> fragile/nonexistent, and as I said, only one direction of communication is
> possible. ?We are currently working on implementing communication with
> javascript (and eventually flash, though this is not a priority)
> objects/methods from within R. This will allow us to draw to draw directly
> to javascript canvases from within an R process, among many other useful
> (and not so useful) capabilities.
>
> We hope to have a usable alpha/proof-of-concept release with bi-directional
> communication between XUL/javascript and R and a mildly-robust installation
> procedure sometime within the next few weeks (3-6), with a paper, live
> examples, and a more complete/robust package to follow.
>
> Glad to see there is some interest :)
>
> Sincerely,
> Gabe Becker
>
> On Wed, Oct 13, 2010 at 8:30 AM, Wolfgang Huber <whuber at embl.de> wrote:
>
> >
> > Since now many browsers support (ECMA/Java-)scripted SVG, I am wondering
> > whether there are already any examples of inserting R code into SVG
> > documents (or a Javascript canvas?) either directly, or perhaps more likely
> > through a JavaScript layer, to dynamically generate graphics or make them
> > interactive?
> >
> > I am aware of the excellent packages gridSVG and SVGAnnotation, which
> > facilitate making R-generated SVG plots more interesting either at
> > construction time or by postprocessing; the above question is about
> > employing R at viewing time.
> >
> > Best wishes
> >
> > Wolfgang Huber
> > EMBL
> > http://www.embl.de/research/units/genome_biology/huber
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Scripting SVG with R
Shane Conway