[R] Lattice plot within a "for" loop does not happen?

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri May 13 16:52:36 CEST 2005

Barry Rowlingson wrote:

>> BUT, when I stick this in a loop, I get a bunch of blank graphics  
>> devices. This happens even if the loop only executes once. I could  
>> just go through and do these one by one, but I was curious if I was  
>> overlooking something obvious. Thank you for any advice.
>  You're overlooking something like line 800 of the documentation for 
> xyplot:
>  Value:
>      An object of class ``trellis''. The `update' method can be used to
>      update components of the object and the `print' method (usually
>      called by default) will plot it on an appropriate plotting device.
>  xyplot doesn't actually make any marks on the screen. Oh no. It returns 
> an object. You have to make that object make the marks on the screen. 
> This happens automatically when you run something interactively, but not 

Baz, actually, printing happens only under two circumstances, AFAIK:
1. by wrapping in print()
2. automatically by evaluating an expression (which might be the object 
name only) in the top level (R_GlobalEnv), but only if no assignment 
takes place.

In particular, automatical (point 2 above) printing happens also in 
non-interactive sessions like

The idea of returning an object is very nice, I think. You can calculate 
on the object and print the modified object (well, in fact, I rarely use 
lattice myself, though).


> inside a function.
>  So wrap your xyplot call in a print() function inside your loop:
>  for(i in 1:10){
>    print(xyplot(....whatever....))
>  }
>  Its probably in the R-FAQ as well, since my original feeling was that 
> this behaviour was chosen in order to confuse people and see how many 
> people read the FAQ... :)
> Baz
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list