[R] Make playwith a default graphic device

Felix Andrews felix at nfrac.org
Mon Oct 15 10:02:55 CEST 2007


My previous suggestion was inconsistent with the Trellis/Lattice idea
of creating a trellis object without necessarily creating a plot. And
it also interfered with attempts to plot to a file device. So here is
a better solution, based on replacing `print.trellis`, though it is
still basically a hack.

library(lattice)
library(plotAndPlayGTK)

setAutoPlaywith <- function(on=TRUE) {
	if (on == FALSE) {
		return(rm(print.trellis, envir=.GlobalEnv))
	}
	assign("print.trellis",
	function(x, position = NULL, split = NULL, more = FALSE, newpage = TRUE,
		packet.panel = packet.panel.default, draw.in = NULL, ...)
	{
		dev.interactive2 <- function(orNone) dev.interactive(orNone) ||
			(interactive() && .Device == "null device" &&
			getOption("device") == "Cairo")
		playing <- 'plotAndPlayUpdate' %in% sapply(sys.calls(), function(x)
			ifelse(is.symbol(x[[1]]), toString(x[[1]]), ""))
		new <- newpage && is.null(draw.in) &&
			!lattice:::lattice.getStatus("print.more")
		if (require(plotAndPlayGTK, quietly=TRUE) &&
			dev.interactive2(TRUE) && !playing && new) {
			# starting a new plot on an interactive device
			eval.parent(call("playwith", x$call), n=2)
			return(invisible())
		}
		# call the real `print.trellis`, from lattice package
		ocall <- sys.call()
		ocall[[1]] <- quote(lattice:::print.trellis)
		eval.parent(ocall)
	}, envir=.GlobalEnv)
	invisible()
}

setAutoPlaywith(TRUE)
xyplot(Sepal.Length ~ Sepal.Width | Species, data=iris)
setAutoPlaywith(FALSE)
xyplot(Sepal.Length ~ Sepal.Width | Species, data=iris)

Deepayan, what do you think -- would it be appropriate to make a
Lattice option for something like this?

Felix

On 10/14/07, Felix Andrews <felix at nfrac.org> wrote:
> Hi Ronaldo,
>
> As Professor Ripley explained, playwith is a function, not a device.
> It uses a Cairo-based device from the cairoDevice package. I too would
> like to have `playwith` as the default behavior for some plots, but
> there is no standard way to do that. It can not be implemented as a
> device because it needs to know what the high-level plot call is.
>
> The only way I can think of is to redefine the generic plot
> function(s). But that is an ugly hack, and can never cover all cases.
> You could do it with the standard Lattice high-level plot functions,
> as follows:
>
> library(lattice)
>
> latticePlay <- function(x, data, ...) {
>         playing <- 'plotAndPlayUpdate' %in%
>                 sapply(sys.calls(), function(x) toString(x[[1]]))
>         if (require(plotAndPlayGTK, quietly=TRUE) && !playing)
>                 return(playwith(plot.call=sys.call()))
>         eval(sys.call(), as.environment("package:lattice"))
> }
>
> latticeFns <- c("barchart", "bwplot", "cloud", "contourplot",
>         "densityplot", "dotplot", "histogram", "levelplot",
>         "parallel", "qq", "qqmath", "splom", "stripplot",
>         "wireframe", "xyplot")
>
> ## this will mask all the high-level Lattice generics
> invisible(lapply(latticeFns, assign, latticePlay, env=.GlobalEnv))
>
> ## now `playwith` is called automatically:
> xyplot(Murder ~ Population, data.frame(state.x77))
>
> ## to revert (remove definitions from the Global Env)
> rm(list=latticeFns)
>
> Note that this approach does not allow you to specify any of the
> optional arguments to playwith().
>
> I would be much more reluctant to try the same thing with base
> graphics (`plot`, etc) because it would cause problems with
> multi-figure layouts.
>
> I have had several reports of the "figure margins too large" error,
> but have not experienced it myself. It seems to happen on unix-like
> systems (macOS / linux) and not on windows. The default window size is
> supposed to be 640x480 pixels, which should be large enough...
>
> Best,
> Felix
>
>
> On 10/14/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> > You seem to be under the misapprehension that playwith() is a graphics
> > device: it is not.
> >
> > To set a graphics device as the default, use options(device=).  I am not
> > sure what 'more functionalities' you are looking for: the playwith() uses
> > the cairoDevice package for its graphics device, and there is also the
> > Cairo package.
> >
> > On Sat, 13 Oct 2007, Ronaldo Reis Junior wrote:
> >
> > > Hi,
> > >
> > > I looking for a graphic device on Linux with more functionalities than the X11
> > > (). I find the plotAndPlayGTK package and the playwith function. It is a good
> > > device. It is possible to make it default in my system.
> >
> > Did you mean that to be a question?
> >
> > > Now I need:
> > >
> > >> library(plotAndPlayGTK )
> > > Loading required package: RGtk2
> > > Loading required package: cairoDevice
> > >>
> > >> playwith(plot(1,1))
> > > Error in plot.new() : figure margins too large
> > >
> > > I got this error above, I dont know why? But in the device I edit call ant it
> > > work.
> > >
> > > But I try to put it default and make a more simple use llike this:
> > >
> > > plot(1,1) and it use a playwith device to make a plot.
> > >
> > > It is possible?
> > >
> > > Thanks
> > > Ronaldo
> > > --
> > >> Prof. Ronaldo Reis Júnior
> > > |  .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional
> > > | : :'  : Campus Universitário Prof. Darcy Ribeiro, Vila Mauricéia
> > > | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil
> > > |   `- Fone: (38) 3229-8187 | ronaldo.reis at unimontes.br | chrysopa at gmail.com
> > > | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> > --
> > Brian D. Ripley,                  ripley at stats.ox.ac.uk
> > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> > University of Oxford,             Tel:  +44 1865 272861 (self)
> > 1 South Parks Road,                     +44 1865 272866 (PA)
> > Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
> --
> Felix Andrews / 安福立
> PhD candidate
> Integrated Catchment Assessment and Management Centre
> The Fenner School of Environment and Society
> The Australian National University (Building 48A), ACT 0200
> Beijing Bag, Locked Bag 40, Kingston ACT 2604
> http://www.neurofractal.org/felix/
> 3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8
>


-- 
Felix Andrews / 安福立
PhD candidate
Integrated Catchment Assessment and Management Centre
The Fenner School of Environment and Society
The Australian National University (Building 48A), ACT 0200
Beijing Bag, Locked Bag 40, Kingston ACT 2604
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8


More information about the R-help mailing list