I have a strange issue with Rstudio: If a script calls ggplot2 functions to display a plot, then using Source to run the script does not produce the plots. If I select the whole script with Ctrl+A
, then Run the current line or selection (Ctrl+Enter
), then the plot does display. Likewise, typing plotting commands into the console produces correct output.
For example:
library(ggplot2)
p = ggplot(mtcars, aes(wt, mpg))
p + geom_point()
Will only produce output if pasted into console, not if sourced.
There are other questions about this, but neither is helpful:
- ggplot2 ggsave function causes graphics device to not display plots falsely claims the issue is fixed in newer versions, it has not.
- RStudio - ggplot not saving first plot when printing and saving multiple plots in a script was closed as a duplicate, yet not only is it not a duplicate, but the
dev.off()
workaround doesn't work ("Error in dev.off() : cannot shut down device 1 (the null device)
")
How can I get Rstudio to display plots when a script is sourced? I am using Rstudio 0.98.1062 and R 3.1.1.