Urgent Assistance Required : Error in pdf("x.pdf") : cannot open file 'x.pdf'

Hi all,

I updated my RStudio to R 4.1.2 this week and since then, I have been unable to create any PDF for saving plots. This error occurs when I attempt to create a pdf; using full file path, no full path (to save to current working directory) and even when using Cairo. An example of the error message I am receiving is Error in pdf("x.pdf") : cannot open file 'x.pdf'.

As I am currently attempting to complete an assignment, any assistance would be greatly appreciated.

Hi,

It's very hard to know what's going on based on just a description. Can you try and post a small example? You can use the reprex guide

A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Just to be sure: The PDF you're trying to write to is not opened on your computer right? Because often times these files get locked when opened, and no other software can overwrite it while this is the case.

PJ

Hi,

Thanks for the quick response. Unfortunately, the PDF I am trying to write is not opened on my computer. Below, I have attached an example where I have used ggplot to plot two variables from the iris dataset. Then, when I go to create a new pdf, to store the plot in, I get the error message shown in bold. Note that this error still occurs whether I specify the directory, or use ggsave. Thanks, again!

p <- ggplot(data = iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point()
p
pdf(file = "iris_plot.pdf")

Error in pdf(file = "iris_plot.pdf") : cannot open file 'iris_plot.pdf'

Hi again,

you have to close the pdf writing after you open it.

library(ggplot2)
p <- ggplot(data = iris, aes(x=Petal.Length, y=Petal.Width)) + geom_point()

pdf(file = "iris_plot.pdf") #Open writing connection
p #plot
dev.off() #close connection

Hope this helps,
PJ

Unfortunately, that is not my error as I am unable to even create a pdf. I receive the error message * Error in pdf(file = "iris_plot.pdf") : cannot open file 'iris_plot.pdf'* as soon as I use the function pdf( ) .

Have you got any idea why this is the case?

Hi,

I'm not sure as this is working on my machine. Have you tried running the code I provided after restarting R studio and changing the filename to something you have not used before?

PJ

I'm afraid I have tried these steps, alongside countless different file names.

The issue lies in the fact that calling the pdf( ) function does not work at all. Similarly, I have checked my file path, tried dev.off() etc.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.