[R] plot via xyplot not being saved

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Sat Jun 16 06:26:34 CEST 2007


On 6/15/07, Benilton Carvalho <bcarvalh at jhsph.edu> wrote:
> So, if those statements are inside a function, I have to make my
> function to have an 'echo' argument/functionality? eg.:
>
> ## begin test.R
> test <- function(n){
>    y <- rnorm(n)
>    x <- rnorm(n)
>    z <- sample(letters[1:4], n, rep=T)
>    library(lattice)
>    bitmap("tst.png")
>    xyplot(y~x|z)
>    dev.off()
> }
>
> test(100)
> ## end test.R
>
> source("test.R", echo=T)
>
> also fails in this case...

Yes. The following will produce some output (the values of x + y and x
- y) if you type it out at the R prompt:

x <- rnorm(10)
y <- rnorm(10)
x + y
x - y

If you put that in a file and source it, nothing will get printed,
unless you have echo=TRUE. If you define

test <- function(){
    x <- rnorm(10)
    y <- rnorm(10)
    x + y
    x - y
}

calling test() at the R prompt will only print x - y and not x + y, and so on.

This is all standard R behaviour. If you want something to be printed
irrespective of context, use print(), e.g.

print(x + y)

or

print(xyplot(y~x|z))

This is also mentioned in the R FAQ.

-Deepayan



More information about the R-help mailing list