27

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()
rjzii
  • 14,236
  • 12
  • 79
  • 119
  • 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`??? – Gavin Simpson Jun 30 '11 at 16:12
  • @Gavin Simpson - Close, now there is a `Rplot001.png` file that is being generated and not cleaned. – rjzii Jun 30 '11 at 16:38
  • 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()` – Aaron left Stack Overflow Jul 01 '11 at 02:20
  • @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. – rjzii Jul 01 '11 at 12:03
  • Sounds like a strong possibility then that there's some code that opens a device without first opening a file, as in my answer. – Aaron left Stack Overflow Jul 01 '11 at 14:42

4 Answers4

36

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.

Syamanthaka
  • 1,227
  • 2
  • 15
  • 23
  • 1
    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. – user3799203 Jun 15 '21 at 13:25
  • Per [base::options documentation](https://stat.ethz.ch/R-manual/R-patched/library/base/html/options.html), it looks like the environment variables R_INTERACTIVE_DEVICE and R_DEFAULT_DEVICE might also be used to address this issue. – user3799203 Jun 15 '21 at 13:36
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.

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • 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. – rjzii Jul 13 '11 at 19:41
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...

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • That goes a long way to solving the problem, but it looks like `options(device = "png")` is also needed to get things running correctly. – rjzii Jun 30 '11 at 15:02
  • 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. – rjzii Jun 30 '11 at 19:35
  • Sorry, no. I've never used `Cairo`. :-( – Andrie Jun 30 '11 at 20:24
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.