[R] jpeg, dev.off() inside function

marciarr marcia.rocha at gmail.com
Tue Aug 19 15:09:26 CEST 2008


Dear all,
I am trying to plot and save the plots. I have written a small function for
that in which I use the jpeg and dev.off() commands. Strangely, when these
commands are inside the function, they are not working anymore.
For instance:

dataframe<- data.frame (x=c(1:20),y=seq(0,9.5, length=20))

for (i in seq(0,15, length=4)){
        setwd ("C:/R")
        jpeg(filename = paste("test ",deparse(substitute(series)),
        i, ".jpg"), width = 1200,
        height = 800,pointsize = 12, quality = 400, bg = "white")
        plot(dataframe[1+i:((nrow(z)/4)+i),], type="l")
        dev.off()} 

This works perfectly. But if I try to make a function out of it, such as:

X<-function (x, y = NULL, plot = TRUE, na.action = na.fail)
{
       if (NCOL(x) == 2) {
        series <- deparse(substitute(x))
        }
        for (i in seq(0,15, length=4)){
        setwd ("F:/R/P to B manuscript/area under the curve")
        jpeg(filename = paste("test ",deparse(substitute(series)),
        i, ".jpg"), width = 1200,
        height = 800,pointsize = 12, quality = 400, bg = "white")
        plot(z[1+i:((nrow(z)/4)+i),], type="l")
        dev.off()}
        return(dataframe)}

and try to run:
X(dataframe)
I get this error message:

Error in jpeg(filename = paste("test ", deparse(substitute(series)), i,  : 
  unable to start device devWindows
In addition: Warning message:
In jpeg(filename = paste("test ", deparse(substitute(series)), i,  :
  Unable to open file 'test  "dataframe" 0 .jpg' for writing

Could anyone help me???
Thank you and have a nice day,
Marcia
-- 
View this message in context: http://www.nabble.com/jpeg%2C-dev.off%28%29-inside-function-tp19048365p19048365.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list