user1700890
user1700890

Reputation: 7730

RStudio vs. plain R editor - saving plot

The following code works in plain R editor (that comes with R) and does not work in RStudio

pdf('test.pdf')
plot(seq(1:10), seq(1:10))
dev.off()

When I run it in RStudio I get in Acrobat "There was an error opening this document. This file is already open or in use by another application"

I get the same error if I use:

pdf('test.pdf')
to_save<-plot(seq(1:10), seq(1:10))
print(to_save)
dev.off()

I have R version 3.3.3 and RStudio: Version 1.0.136 – © 2009-2016 RStudio, Inc. This is on Win 7.

Any ideas how to troubleshoot?

Upvotes: 0

Views: 204

Answers (2)

Javier Fajardo
Javier Fajardo

Reputation: 797

It works fine on my RStudio. In my experience, that message when opening the pdf appears if the connection to the file has not been closed. Do you get the message

null device
          1

when executing dev.off()?

Upvotes: 1

Alex Gold
Alex Gold

Reputation: 355

If you're ok with ggplot2, ggsave is a great option:

ggplot(data.frame(seq(1:10), seq(1:10)), 
       aes(x = x, y = y)) + 
       geom_point()
ggsave("test.pdf")

Upvotes: 1

Related Questions