[R] Decreasing my personal entropy ...

John Fox jfox at mcmaster.ca
Fri Jan 31 15:12:02 CET 2003


Dear Bert,

At 08:45 AM 1/31/2003 -0500, Gunter, Bert wrote:
>R-Listers:
>
>A very minor -- and maybe silly -- question just for personal enlightenment.
>
>In S (either R or S-Plus, AFAIK) when one types or pastes a trellis graphics
>command into the commands/console window, the graph is automatically
>produced:
>e.g.,
>
>trellis.device(...)
>xyplot(y~x)
>
>If one puts these in a function and calls the function, the same occurs.
>However, if one sources in these command from a file (i.e., using source()),
>automatic printing does not occur; one must explicitly call
>print(xyplot(y~x)) . Why do things work this way?

Actually, a function that calls xyplot() won't necessarily produce a plot 
without an explicit call to print(). Consider the following two functions:

         fun.1 <- function(x,y) {xyplot(y~x); return(NULL)}
         fun.2 <- function(x,y) xyplot(y~x)

fun.1 doesn't plot anything; fun.2 produces a plot when called from the 
command prompt, but only when its result is unassigned, and only because it 
returns an object of class "trellis", which is then automatically printed.

The general point is that the print method for trellis objects plots the 
objects, so when print() gets called implicitly on a trellis object, the 
object is plotted.

I hope that this helps,
  John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------




More information about the R-help mailing list