How do I prevent Rplots.pdf from being generated?
Asked Answered
S

4

28

I am working with some R code that generates a number of images as png files; however, a Rplots.pdf file keeps on being generated in the working directory, is there a way to prevent this from happening?

library(Cairo)
CairoPNG(file = "graphs.png")
nf <- layout(matrix(c(1:8), 2, 4, byrow=T), c(1, 1), c(1, 1, 1, 1), TRUE)
for (k in 1:num.k) {
    plotMatrix(connect.matrix.ordered[k,,], log = F, main = paste("k=", k.vector[k]), sub = paste("Cophenetic coef.=", rho[k]), ylab = "samples", xlab ="samples")
}
y.range <- c(1 - 2*(1 - min(rho)), 1)
plot(k.vector, rho, main ="Cophenetic Coefficient", xlim=c(k.init, k.final), ylim=y.range, xlab = "k", ylab="Cophenetic correlation", type = "n")
lines(k.vector, rho, type = "l", col = "black")
points(k.vector, rho, pch=22, type = "p", cex = 1.25, bg = "black", col = "black")
dev.off()
Sporades answered 30/6, 2011 at 13:58 Comment(5)
Did you mean Rplots.png?? I can't see how @Andrie's answer would stop a PDF device being created, but can see it being a solution if you actually mean Rplots.png???Homager
@Gavin Simpson - Close, now there is a Rplot001.png file that is being generated and not cleaned.Sporades
I can't reproduce your problem (in part because your example isn't self-contained). Does something simpler like the following also produce the rogue file? library(Cairo); CairoPNG(file = "graphs.png"); layout(matrix(c(1:4), 2)); for (k in 1:4) plot(1,k); dev.off()Habitue
@Aaron - Not sure myself yet either. The code is largely undocumented and there is a lot of it related to plotting charts that we don't even need any more. I might have to update the question again once I clean some more code out.Sporades
Sounds like a strong possibility then that there's some code that opens a device without first opening a file, as in my answer.Habitue
H
15

I wonder if you have another command that opens a device before or after the code snippet you've given us. When you're all done run dev.cur() to see if there was a device left open. If not, it should return the null device.

Here are ways you can recreate getting a Rplots.pdf or a Rplot001.png; the layout and par commands open a device if one isn't open, and since no filename has been given, it uses the default filename.

options(device="pdf")
layout(1:4)
dev.off()

options(device="png")
par()
dev.off()

Maybe seeing that happen here will give you a clue as to what's happening with your code.

Habitue answered 1/7, 2011 at 2:28 Comment(1)
This ended up being the problem - a plot being generated without a file being opened up for it. Very obscure location in the code though.Sporades
T
38

I know this is a very old post and surely the OP has solved this. But I encountered this similar situation while working with plotly. Converting a ggplot output into a plotly output generated the similar error of not being able to open file 'Rplots.pdf'.

I solved it by simply including :

pdf(NULL)

I'm not sure of the reason for the error, have not been able to figure that out, but this small line helped removing the error and displaying my plots as I would expect in plotly and ggplot combinations.

Tiptop answered 27/7, 2016 at 6:59 Comment(2)
Great tip, @Syamanthaka. This had bugged me for a long time. But I found that while pdf(NULL) suppresses Rplots.pdf from being created when R is run in batch mode, but also suppresses the Plots window display in RStudio, if pdf(NULL) is run before any plot is displayed there. You can avoid the RStudio problem by using if(!interactive()) pdf(NULL) near the top of a program that you may run in batch or interactively.Sundried
Per base::options documentation, it looks like the environment variables R_INTERACTIVE_DEVICE and R_DEFAULT_DEVICE might also be used to address this issue.Sundried
H
15

I wonder if you have another command that opens a device before or after the code snippet you've given us. When you're all done run dev.cur() to see if there was a device left open. If not, it should return the null device.

Here are ways you can recreate getting a Rplots.pdf or a Rplot001.png; the layout and par commands open a device if one isn't open, and since no filename has been given, it uses the default filename.

options(device="pdf")
layout(1:4)
dev.off()

options(device="png")
par()
dev.off()

Maybe seeing that happen here will give you a clue as to what's happening with your code.

Habitue answered 1/7, 2011 at 2:28 Comment(1)
This ended up being the problem - a plot being generated without a file being opened up for it. Very obscure location in the code though.Sporades
N
2

Here is the source code for CairoPNG:

function (filename = "Rplot%03d.png", width = 480, height = 480, 
    pointsize = 12, bg = "white", res = NA, ...) 
{
    Cairo(width, height, type = "png", file = filename, pointsize = pointsize, 
        bg = bg, ...)
}

This tells you that CairoPNG takes filename=... as a parameter, and passes this to Cairo as the file parameter.

I can see how this can lead to confusion, but the point is that your call to CairoPNG should be:

CairoPNG(filename="graphs.png")

See if that works...

Napoleonnapoleonic answered 30/6, 2011 at 14:11 Comment(3)
That goes a long way to solving the problem, but it looks like options(device = "png") is also needed to get things running correctly.Sporades
Did some more checking and the when the options(device = "png") is on a Rplot001.png file is generated, likewise, if it is off the Rplots.pdf is generated. Any ideas what might be going on? The Rplot001.png file does have valid data in it from an aggregate report.Sporades
Sorry, no. I've never used Cairo. :-(Napoleonnapoleonic
B
1

I had a similar problem recently after upgrading to R-3.0.3 (yes we're a little behind!). It turns out that palette("default") opens a device now, though it didn't used to.

Bioluminescence answered 29/7, 2016 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.