[R] function to display histogram fails to do so when histogram is not the last expression in the function

David Winsemius dwinsemius at comcast.net
Wed Feb 17 20:29:53 CET 2010


On Feb 17, 2010, at 2:13 PM, Terrence Ireland wrote:

> TestHistogram_A  is a function that plots a histogram,

It creates a plot-description but does not "plot" it.

> then returns 3.  For
> me it fails to display the plot, at least in the order given below
>
> TestHistogram_B is a function that plots a histogram, then exits.    
> The only
> difference between
>
> the 2 functions is that B does not return 3.   It displays the plot.
>
> This problem does not occur when histogram is replaced by boxplot

histogram is a lattice function. Lattice functions return a value  
which is a list describing the plot. This phenomenon is covered in a  
FAQ:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f



>
> Their use is in the following statements:
>
> Data <- c(1,1,1,2,2)
> Data <- data.frame(Data)
> TestHistogram_A <- function (my.data) { histogram(my.data[, 
> 1],main="TEST");
> 3}

You have thrown away the histogram in your "_A" function. Boxplot os a  
base graphics function which prints by "side-effect" and returns NULL.  
If you want to print() the histogram, then do so within your function.

> TestHistogram_A(Data)
> TestHistogram_B <- function (my.data) { histogram(my.data[, 
> 1],main="TEST") }
> TestHistogram_B(Data)
>


David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list