[R] awkward behavior with densityplot function

Bill.Venables at csiro.au Bill.Venables at csiro.au
Wed Aug 27 01:20:53 CEST 2008


Lattice functions do not create plots.  In this respect they are quite different to functions like plot(...), hist(...) &c, which do create plots.

If you want to see the plot from a lattice function, you need to print the object it creates.  It's the action of printing that creates the graphical image.

This happens automatically at the command line, but if you want it to happen inside the function you need to use print(...) explicitly.

So your function should be as in the following:

---- t.R ---
grafica <- function() {
   v <- read.csv('preprocessed/komolongma.ece.uprm.edu.active',sep=',')
   x <- as.ts(v$active)
   bitmap(file="output.png")
   print(densityplot(~x,col='blue',main='Density Plot')) ###<<<---
   dev.off()
   invisible(v)  ### further suggestion.
}
grafica()
---- t.R ---
Bill Venables
http://www.cmis.csiro.au/bill.venables/


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of John Sanabria
Sent: Wednesday, 27 August 2008 9:08 AM
To: r-help at r-project.org
Subject: [R] awkward behavior with densityplot function

Hi,

I have the following script:
---- t.R ---
grafica <- function() {
   v <- read.csv('preprocessed/komolongma.ece.uprm.edu.active',sep=',')
   x <- as.ts(v$active)
   bitmap(file="output.png")
   densityplot(~x,col='blue',main='Density Plot')
   dev.off()
}
grafica()
---- t.R ---

When I "sourced" it from R prompt, it quietly runs. However the
"output.png" generated contains any visible data. I said that, because
the file is created and it has 2062 bytes.
--- execution ---
 > library(lattice)
 > source(file="t.R")
 >
--- execution ---

Now, if I sequentially run the lines above in the R prompt, the
"output.png" file contains a graphical representation of the data.
--- execution 2 ----
 > v <- read.csv('preprocessed/komolongma.ece.uprm.edu.active',sep=',')
 > x <- as.ts(v$active)
 > bitmap(file="output.png")
 > densityplot(~x,col='blue',main='Density Plot')
 > dev.off()
---- execution 2 ---

What is wrong with the densityplot function that produces any output
when is invoked from a script?
Someone has a script invoking the densityplot function?

Thanks a lot for your help.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list