Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot plots in scripts do not display in Rstudio

Tags:

r

ggplot2

rstudio

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.

like image 819
Superbest Avatar asked Oct 30 '14 02:10

Superbest


People also ask

Why is R studio not showing plots?

Go to Tools->Global Options->Rmarkdown. In "Show output preview in" select "Viewer Pane" Uncheck the box "Show output inline for all R Markdown documents"

Why is my ggplot not working?

3 if you see Error in ggplot(...) : could not find function "ggplot" , it means that the ggplot() function is not accessible because the package that contains the function ( ggplot2 ) was not loaded with library(ggplot2) . Thus you cannot use the ggplot() function without the ggplot2 package being loaded first.

Why is my ggplot blank?

A blank ggplot is drawn. Even though the x and y are specified, there are no points or lines in it. This is because, ggplot doesn't assume that you meant a scatterplot or a line chart to be drawn.


2 Answers

The solution is to explicitly call print() on ggplot object:

library(ggplot2)  p <- ggplot(mtcars, aes(wt, mpg)) p <- p + geom_point() print(p) 

ggplot function returns object of class ggplot; ggplot2 works by overloading print function to behave differently on objects of class ggplot - instead of printing them to STDOUT, it creates chart.

Everything is working well in interactive mode, because R assumes that most of commands are run through print() function. This is for our convenience and allows us to type rnorm(1) and get any visible output. When Run current selection command is used (Ctrl+Enter), RStudio behaves as if each selected line was typed in interactive mode and run. You can verify that by checking your command history in Console pane after running few selected lines.

But this convenient mode is abandoned when file is read by source(). Since this function is intended to run (potentially long and computationally-expensive) R scripts, it is undesirable to pollute STDOUT with low-priority messages. That's why source() by default will output only error message. If you want anything else, you have to explicitly ask for that.

like image 116
Mirek Długosz Avatar answered Oct 05 '22 15:10

Mirek Długosz


though it's a quite old question. I had the same problem and found a quick solution, if you want to use "source" button on R studio edit box.

you can simply turn on "source with echo" (Ctrl + Shift + Enter) and the plot shows as expected

like image 36
bcat Avatar answered Oct 05 '22 15:10

bcat