gprocessingjs {gWidgetsWWW}R Documentation

Interface to Processing.js code to create graphics with javascript

Description

This function provides the ability to write to canvas on the browser using somewhat familar graphics commands in R. The processing.js (http://ejohn.org/blog/processingjs) javascript library is used to provide an interface to the canvas web object.

Usage

gprocessingjs(width=400, height=400, pointsize= 12, container = NULL, ...)

Arguments

width width of canvas in pixels
height height of canvas in pixels
pointsize size of font in pixels
container parent container for processing object
... not used

Details

This widget requires external javascript libraries to be installed and called in through in the header of the html page.

The libraries can be downloaded at http://ejohn.org/apps/processing.js/processing.js and http://ejohn.org/apps/processing.js/examples/init.js

The constructor returns a proto object, for which several methods are defined, including many familiar graphics primitives. Methods for proto objects are called through the object-oriented notation obj\$methodname(arguments).

The proto object has properties ..margin and ..background that may be changed directly to adjust the defaults.

The main methods have only an abbreviated subset of the graphical parameters available.

The following familiar methods are implemented:

There are also several methods from the processing API that are implemented. The processing API allows for interactive graphics. The example shows how one can use a slider to adjust a histogram.

This can be kind of slow, as the lag between the browser and R can make the interface non-responsive. If one wishes to write some javascript code, the methods "mouseDragged","mouseMoved", "mousePressed", "mouseReleased", "keyPressed","keyReleased","draw","setup" can be set to functions that produce javascript handlers. This way there is not a lag between the browser and the R process.

Value

Returns a proto object with class gProcessing.

Author(s)

John Verzani

References

http://ejohn.org/blog/processingjs

Examples

## Not run: 
   ## make a histogram with breaks controlled by a slider
   w <- gwindow("processing example")
   g <- ggroup(horizontal=FALSE, cont=w)
   x <- faithful$eruptions
   p <- gprocessingjs(width=500, height = 400, container  = g)
   p$textFont("arial",14)
   p$Hist <- function(.,x, breaks = "Sturges", col = "goldenrod") {
     out <- hist(x, breaks = breaks, plot=FALSE)
     p$plot.new()
     p$plot.window(xlim = range(out$breaks), ylim = c(0, max(out$counts)))
     p$axis(1)
     p$title(main = deparse(substitute(x)))
     nb <- length(out$breaks)
     p$rect(out$breaks[1:(nb-1)], rep(0, length=nb-1), out$breaks[2:nb],
       out$counts, col)
   }
   p$Hist(faithful$eruptions)
   gslider(from = 1, to = length(x), by = 1, cont = g, handler =
   function(h,...) {
     p$Hist(faithful$eruptions, breaks =as.numeric(svalue(h$obj)))
   })

 ## Make regression line that follows mouse clicks
 x <- c(); y <- c()
 glabel("Click in graph to set points", container=g)
 p1 <- gprocessingjs(width=500, height = 400, container  = g)
 p1$..background <- 200
 p1$plot.new()
 p1$plot.window(xlim=c(0,10), ylim=c(0,10))
 addHandlerMouseclick(p1, handler = function(h,...) {
   xy <- p1$pixelsToXY(h$xy)
   assign("x",c(x, xy[1]), envir=.GlobalEnv)
   assign("y",c(y, xy[2]), envir=.GlobalEnv)
   p1$plot.new() # paints background
   p1$points(x, y, cex=2, col="red")
   if(length(x) != 1) {
     res <- lm(y ~ x)
     p$abline(res,col="blue")
   }
 })

   visible(w) <- TRUE

## End(Not run)

[Package gWidgetsWWW version 0.0-6 Index]