[R] Graphics device storable in a variable

Josh Tolley eggyknap at gmail.com
Fri Nov 16 05:50:10 CET 2007


On Nov 15, 2007 2:41 PM, Michael Lawrence <lawremi at iastate.edu> wrote:
> This is possible using the cairoDevice package and RGtk2.
>
> Turning an R graphic into a raw vector of bytes:
>
> library(cairoDevice)
> library(RGtk2)
>
> # create a pixmap and tell cairoDevice to draw to it
>
> pixmap <- gdkPixmapNew(w=500, h=500, depth=24)
> asCairoDevice(pixmap)
>
> # make a dummy plot
>
>  plot(1:10)
>
> # convert the pixmap to a pixbuf
>
> plot_pixbuf <- gdkPixbufGetFromDrawable
> (NULL, pixmap, pixmap$getColormap(), 0, 0, 0, 0, 500, 500)
>
> # save the pixbuf to a raw vector
>
> buffer <- gdkPixbufSaveToBufferv(plot_pixbuf, "jpeg", character(0),
> character(0))$buffer
>
> ###
>
> Then you can send buffer to your database, or whatever. Replacing "jpeg"
> with "png" will probably produce png output.
>
> Michael
>

Many thanks to all respondents. I'll play with these.

-Josh



More information about the R-help mailing list