Re: [Rd] [R] Save generic plot to file (before rendering to device)

From: Hadley Wickham <>
Date: Mon, 01 Aug 2011 09:02:59 -0500

Seems like maybe a call is missing in grid.newpage? But I think it should be called multiple times for plot.lm - there are multiple plots there.


On Monday, August 1, 2011, Jeroen Ooms <> wrote:
> Bumping this one up because the '' solution turned out
> to be sub-optimal after all.
>>> It should be possible to do this with a hook, right?
>> Yes, sure, if you treat the first and last plot separately.
> It turns out that the hook does not is not triggered
> at the right moments. I'm not sure if this is intended behavior or
> incorrect implementation. What I was expecting is a hook/event that is
> triggered every time before a new graphics frame is opened. E.g. if
> there is an open PDF device and some plots are printed, the number of
> times the hook is called should be exactly equal to the number of
> pages in the resulting PDF document. Sometimes this works as expected,
> sometimes it doesn't.
> At the end of this message some example code. In the first example,
> the hook works as expected is called 4 times, as there are 4 plots. In
> all the other examples the event is either triggered too often or not
> triggered at all. I guess the hook is called when the
> function is explicitly called, which might not always happen.
> My question would be if (1) this is the intended behavior for
> '', and (2) if yes, would it be possible to define an
> additional event that always triggers, and only triggers, if a
> completely new graphics device is opened. I.e. whenever a pdf device
> would start a new page.
> Thank you.
> #set the hook (event listener)
> setHook("", NULL);
> setHook("", function(){ message("Yay! A new plot!")});
> #works as expected:
> plot(lm(speed~dist, cars), ask=F);
> #triggered way too often, once for every partition of the plot
> plot(mtcars);
> #not triggered at all by lattice
> library(lattice);
> dotplot(speed~dist, cars);
> #not triggered at all by ggplot2
> library(ggplot2);
> qplot(speed, dist, data=cars);

Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

	[[alternative HTML version deleted]]

______________________________________________ mailing list
Received on Mon 01 Aug 2011 - 14:07:09 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 02 Aug 2011 - 08:50:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive