Using jpeg (file =) creates an empty jpeg even if there is no call to generate the graph

This is observed in the party. Considering the following code snippet, I don't understand why R creates an empty jpeg file on Windows, even though I am not naming a graph or graph. When I run similar code under Linux or OS X, no jpeg file is generated. I don't know in advance if the user wants to plot, so I set the filename in advance and give them a name and location.

##
sink("c:\\temp\\test.lst")
jpeg(file="c:\\temp\\test%d.jpeg")
norm <- rnorm(100)
print(norm)

      

Any suggestions would be appreciated.

+3


source to share


2 answers


The help file ?jpeg

(which also applies to devices bmp()

, png()

and tiff()

) indicates that:

The โ€˜type = "windows"โ€™ versions of these devices effectively plot
 on a hidden screen and then copy the image to the required format.

      



This Windows-specific implementation detail most likely explains the difference in behavior between Windows and * NIX systems.

On Windows, calling any of the above functions (and pdf()

and postscript()

) creates a file --- regardless of whether you draw anything on that hidden screen. Except for pdf()

(which creates files that I cannot open with the viewer), the image registered to the build device is a white rectangle of height and width specified when the specific device is called.

+4


source


I wouldn't bother with that. if the prospect of zero-length files cluttering a folder bothers you, you can clean up after which something like



jpgs <- file.path("c:/temp", dir(pattern="c:/temp/test[0-9]*\\.jpeg"))
s <- file.info(jpgs)[["size"]]
for(i in seq_along(s))
    if(s[i] == 0) file.remove(jpgs[i])

      

+2


source







All Articles