sam
sam

Reputation: 541

Make R Studio plots only show up in new window

When using R Studio, I usually just work with an .R file stacked on top of the Console. I keep the other panes (Environment, History, Files, etc) hidden.

But whenever I plot a graph, the other panes automatically pop out of the side bar to show me the Plot pane. Because I work on a laptop, this makes everything too small to see. By clicking the Zoom button on the Plots pane, I can get the plot also show up in a new window, but does not prevent the Plots pane from showing up.

Is there a way to "disable" the Plots pane in R Studio, and force plots show up in a new window?

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1     jsonlite_1.4     
[3] data.table_1.10.4

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6
 [3] scales_0.4.1     lazyeval_0.2.0  
 [5] plyr_1.8.4       tools_3.2.3     
 [7] gtable_0.1.2     tibble_1.3.0    
 [9] curl_2.5         Rcpp_0.12.10    
[11] grid_3.2.3       munsell_0.4.2   
> 

Upvotes: 23

Views: 80244

Answers (6)

BGranato
BGranato

Reputation: 169

If you want all plots in the current script to appear on a separate window, this should do the job:

dev.new(noRStudioGD = TRUE)

*Tested on RStudio version 1.4.1106 for Windows with R version 4.0.5 (2021-03-31)

For a 'permanent' solution, the answer by Rubén Fernández-Casal should work well.

Upvotes: 4

Commenting the following lines in C:\Program Files\RStudio\R\Tools.R seems to work (it may be necessary to edit the file as administrator):

# set our graphics device as the default and cause it to be created/set
.rs.addFunction( "initGraphicsDevice", function()
{
   # options(device="RStudioGD")
   # grDevices::deviceIsInteractive("RStudioGD")
  grDevices::deviceIsInteractive()
})

Upvotes: 9

JegarP
JegarP

Reputation: 554

The dev.new() function will open a new plot window, which then becomes the target for all plots.

If you wish to open another window you can run the command a second time to open a second window.

dev.off() will shut down the window (in the order they were opened by default).

You can see how to control multiple graphics devices in the documentation here.

Upvotes: 23

user2554330
user2554330

Reputation: 44788

In RStudio, the default graphics device is normally "RStudioGD". You can change that to something else: the normal choices are "windows" on Windows, "quartz" on MacOS, "X11" on Linux. So for example, use

options(device = "quartz")

in your RStudio session on a Mac and you'll get the regular MacOS graphics window.

Upvotes: 16

Jul
Jul

Reputation: 1139

Try using the windows command before your plot call.

windows();(mpg ~ wt, mtcars)

The plot should pop-up in its own window whilst the pane stays minimized.

Upvotes: 10

Samuel
Samuel

Reputation: 3053

You can force RStudio to show plots in the Source window if you use R Markdown. In a Rmd file, plots are shown together with code; it's called an R Markdown notebook. You can set the size of the plots too, in what is called an R code chunk:

```{r fig.height = 2, fig.width = 3}
plot(mpg ~ wt, mtcars)
```

When you run the chunk, the plot is shown below it.

If you want to set the plot size for the whole notebook, set the package option using opts_knit and opts_chunk, for example:

```{r setup} 
library(knitr) 
opts_knit$set(global.par = TRUE) 
opts_chunk$set(fig.width = 4.5, fig.height = 3.5)
```

For more information, see here and here.

Upvotes: 2

Related Questions