problem saving pdf file in R with ggplot2

ggplot2, macos, r

Solution

The problem is that you don't close the `pdf()` device with `dev.off()`

dat <- data.frame(A = 1:10, B = runif(10))
require(ggplot2)

pdf("ggplot1.pdf")
ggplot(dat, aes(x = A, y = B)) + geom_point()
dev.off()

That works, as does:

ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("ggplot1.pdf")

But don't mix the two.

Problem

I am encountering an odd problem. I am able to create and save pdf file using R/ggplot2 and view them while the R Console is running. As soon as I exit the R console, Preview on Mac OS X will no longer display the PDF. I have been able to save .png files w/o problem, but for reasons beyond my control, I need to save in pdf files. The code I am using to save is as follows: ``` pdfFile <-c("/Users/adam/mock/dir/structure.pdf") pdf(pdfFile) ggplot(y=count,data=allCombined, aes(x=sequenceName, fill=factor(subClass))) + geom_bar() ggsave(pdfFile) ``` Has anyone encountered a similar problem? If so, what do I need to do to fix it? Thank you very much for your time.

Original source