Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Rplots.pdf from being generated?

Tags:

r

cairo

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()
like image 547
rjzii Avatar asked Jun 30 '11 13:06

rjzii


3 Answers

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.

like image 117
Syamanthaka Avatar answered Nov 12 '22 18:11

Syamanthaka


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.

like image 38
Aaron left Stack Overflow Avatar answered Nov 12 '22 20:11

Aaron left Stack Overflow


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...

like image 2
Andrie Avatar answered Nov 12 '22 18:11

Andrie