Reputation: 51
When I try to save a plot made using ggplot as a pdf using this code:
library(ggplot2)
file = "/data/mda/20150630-1Mb-full_comparison-low_depth_hTERT/result/comparison_figure/SD_rank_custom.csv"
figure_file = "/data/mda/20150604-1Mb-full_comparison-low_depth_hTERT/result/comparison_figure/SD_rank_custom.pdf"
sd_data <- as.data.frame(read.csv(file, header=TRUE))
# generate box plot
ggplot(
data=sd_data,
aes(
x=Experiment,
y=SD
)
)+
theme_bw() + #use bw theme
geom_boxplot(outlier.shape = NA) + #hide outlier points
geom_jitter() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave(
filename=figure_file,
width=10,
height=10
)
I get the following error:
Error in grDevices::pdf(..., version = version) :
cannot open file 'file.pdf'
Calls: ggsave -> device ->
Execution halted
I think my version of R was recently updated to 3.2.0, and I've confirmed that it works fine in v3.1.1 so I'm assuming this is version related. I also confirmed I can write a csv file to the directory.
Any ideas how to fix this?
Upvotes: 5
Views: 17728
Reputation: 1
I had the same problem, I fixed it by moving the working directory, since it thought it was in system32.
If you are working in Rstudio
and either make a new folder or choose an existing one
Upvotes: 0
Reputation: 537
I got the same error when my file name was too long - and therefore the path name in windows exceeded the allowed letters limit. Shortening the file name solved this issue.
Upvotes: 1
Reputation: 21
I just got this error while running a for loop to create many pdf files containing charts.
For me, the error was resolved when I simply closed out of the one pdf file I opened earlier to examine my output. R was throwing the error because I had the file open, so it could not overwrite it. It is another plausible cause for this error.
Upvotes: 1
Reputation: 697
T faced the same issue and found out that the here() is not pointing to the correct directory in which the R script file is. Solution to this is, either try to point the here() to the correct directory or close\quit the R studio and open the script directly in R studio i.e. Open the R studio fresh then, here() will automatically point to the same directory as to that of the script.
Upvotes: 0
Reputation: 843
One of the reasons for Error in grDevices::pdf
is write permissions. Proper write permissions should solve the issue. - chmod 777 -R dirname
Upvotes: 0
Reputation: 31
I had the same, turned out I had already an object with the same name saved in that directory that apparently does not get overwritten.
Upvotes: 3
Reputation: 3694
I think you're trying to write to a non-existent folder, and as far as I know grDevices won't allow this. Someone else seems to have a similar issue.
I tried your code both in R 3.1.1 and 3.2.1—they both give the same error (unless the directory is created on beforehand.
You could try adding the following line of code:
dir.create(file.path(dirname(figure_file)))
It will create the directory for you.
Upvotes: 3