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

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 19 18:21:46 CEST 2008


On Tue, 19 Aug 2008, Henrik Bengtsson wrote:

> In your function, split up your code in two lines as:
>
> pathname <- paste("test ", deparse(substitute(series)), i, ".jpg");
> jpeg(filename=pathname, width=1200, height=800, pointsize=12,
> quality=400, bg="white");
>
> The focus on what 'pathname' is/becomes when you put it inside your
> function.  That will help you troubleshoot.  It has most likely
> nothing to do with the jpeg() function.

It isn't:

>>  Unable to open file 'test  "dataframe" 0 .jpg' for writing
                               ^^         ^^ ^
and " is invalid in a Windows filename.  (I also think sep="" was intended 
in paste().  This is because 'series' is 'x' deparsed twice.

>
> My $.02
>
> /Henrik
>
> PS. 'series' is not set inside the function if there are not two columns. DS.
>
>
> On Tue, Aug 19, 2008 at 6:09 AM, marciarr <marcia.rocha at gmail.com> wrote:
>>
>> 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.
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list