[R] dev.new() within a loop

David Winsemius dwinsemius at comcast.net
Sun Nov 13 16:38:13 CET 2011


On Nov 13, 2011, at 9:35 AM, Giovanni Azua wrote:

>
> On Nov 13, 2011, at 3:23 PM, David Winsemius wrote:
>>>> Please read both my comments and the FAQ more carefully . You are  
>>>> inadequately considering the information that has been offered to  
>>>> you.
>>>>
>>> Ok you wanted to make sure I have to read the FAQ well I didn't  
>>> have to :) Googling using your suggestion found relevant matches  
>>> and now it works.
>>
>> Where does this resistance to reading the FAQ come from?
>
> It is not resistance, the FAQ is very helpful for basic general  
> questions but it can not cover all details. Sometimes it is very  
> hard to find a specific detailed answer within a general FAQ. I have  
> read the FAQ, what makes you think I didn't before? I just could not  
> find the information I was looking for.

I told you which FAQ to read. It's only a few sentences. And it very  
specifically and I think clearly covers the problem you asked about. I  
do not know why you had to google it if you really did read that  
particular FAQ.

>
>>> I had to wrap the ggplot call within a "print" for my loop to work  
>>> which is IMHO one of the most obfuscated use-cases for using print  
>>> I have bumped into.
>>
>> It is a case of isolating the functional aspects of the plot- 
>> construction process from the side-effects of interacting with a  
>> graphics device.
>>
>>> So every user-defined function that try to plot anything has to be  
>>> wrapped inside a print just in case it ever gets called from  
>>> within a loop
>>
>> That is not how I understand it. One reason is so there is an  
>> object in the workspace that can be later modified. And I suspect  
>> the authors (and I am not one of them)  imagined that there may be  
>> multiple steps in creation of the  object, not all of which should  
>> result in a plot appearing if this is being done programatically.  
>> This is especially appropriate (it would seem to me) for the ggplot  
>> plotting model, which adds a variety of layers to a core object. It  
>> is also imagined that you may be sending this object to one of a  
>> variety of devices.
>>
> Ok bottom line is always wrap the plot call whatever it is within a  
> print for the just in case.

That is true when you are using grid-based graphic. It is going to  
return a NULL in the case of base graphics calls.

This is the function:

 > print.ggplot
function (x, newpage = is.null(vp), vp = NULL, ...)
{
     set_last_plot(x)
     if (newpage)
         grid.newpage()
     if (is.null(vp)) {
         grid.draw(ggplotGrob(x, ...))
     }
     else {
         if (is.character(vp))
             seekViewport(vp)
         else pushViewport(vp)
         grid.draw(ggplotGrob(x, ...))
         upViewport()
     }
}
<environment: namespace:ggplot2>


-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list