PhilR
PhilR

Reputation: 91

Using jpeg(file= ) creates an empty jpeg even though there's not a call to create a plot

This is observed running in batch. Given the following code snippet, I don't understand why R creates an empty jpeg file in Windows even though I am not calling a plot or graph. When I run similar code under Linux or OS X, the jpeg file is not created. I don't know ahead of time if the user is going to want a plot so I setup the filename(s) ahead of time 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.

Upvotes: 3

Views: 1704

Answers (2)

Hong Ooi
Hong Ooi

Reputation: 57686

I wouldn't worry about it. if the prospect of zero-length files cluttering up the folder bothers you, you can clean up afterwards with 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])

Upvotes: 2

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162321

The ?jpeg help file (which also applies to bmp(), png() and tiff() devices) 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 the behavior of Windows and *NIX systems.

On Windows, calling any of the functions above (and pdf() and postscript() as well) creates a file --- whether or not you then plot anything to that hidden screen. Except for pdf() (which produces files that I can't open with a viewer), the image that's registered on the plotting device is that of a white rectangle of height and width specified in the call to the specific device.

Upvotes: 4

Related Questions