I have code to clear the workspace: rm(list=ls()) and code to clear the console: cat("\014")
Is there code to clear all plots from Rstudio?
dev.off() closes the current graphical device. This clears all of the plots for me in RStudio as long as I don't have a different graphical device open at the moment. If you do have other graphical devices open then you can use dev.list() to figure out which graphical device is RStudio's. The following should do that but I haven't tested it thoroughly.
dev.off(dev.list()["RStudioGD"])
But if you aren't doing anything else then just using dev.off() should take care of it.
dev.off() only works in an interactive session. If you're interested in implementing such behavior in a script, you should use
graphics.off()
instead.
To prevent error message in case there are no plots to clear:
if(!is.null(dev.list())) dev.off()
I usually use
while (dev.cur()>1) dev.off()
and since I use RGL a lot, I often add:
while (rgl.cur()) rgl.close()
I have gone for this which seems to work without reporting any errors:
# Clear all plots
try(dev.off(dev.list()["RStudioGD"]),silent=TRUE)
try(dev.off(),silent=TRUE)
I merged the instructions from the other answers with an answer on error handling here:
Related
I ran into a very weird problem: my R code for generating a plot only works if I type it into the console but not when I ran it inside the script (with Ctrl+Enter command)... It's the same problem with all plots (regular plots or ggplots). Also I tried it on two different computers and the same thing happened. Anyone have any idea why this is happening?
One possible reason: I installed the newest version of Rstudio on both computers so it might be an issue with the version. The exact same code worked before on an older version of rstudio...Could this be it? If so, how can I fix it?
I think I figured out what the problem was: the setting in the new version of Rstudio has a default option of outputting the plots inside the Rmarkdown script (at the very end of the script). And that's why I wasn't seeing them. You could change the setting such that it outputs in the console.
Try dev.off() to reset the graphics device.
This helps with a lot of weird graphics behaviour.
Probably too late for the original poster... However, I just ran into the same problem after installing an R update. The way I fixed it was to go to preferences, R markdown, and turn off "show inline output". For me, it was just coming out at the bottom of the chunk instead of in the plot window like I wanted. Hope that helps someone!
I just ran into this problem. I mistakenly put my plot() command inside the r markdown setup chunk. I moved it to its own code chunk and it ran as expected.
I have a simple R script doing:
jpeg(myplot.jpg)
x<-seq(1,20,0.1)
y<-sin(x)
plot(x,y)
dev.off()
After execution it makes a myplot.jpg file in /root/work/ but renders gibberish information and does not plot a legible graph.
Also how can I view the graph in R shell itself?
The first argument to jpeg should be a character string, so I wouldn't expect your code to work unless myplot.jpg contained a character string. This works fine for me:
jpeg("myplot.jpg")
x<-seq(1,20,0.1)
y<-sin(x)
plot(x,y)
dev.off()
Whether you can view the graph in the "shell" depends on the R console you're using. If you're running R from bash, sh, etc, the answer is "no, you can't view a plot directly"... actually it wouldn't surprise me if there was a package that allowed you to create text-plots, but I don't think that's what you want.
I am familar with matlab, but relatively new to r. I have an r script which produces many different graphical plot windows and takes some time in between each one. While this is running, I tend to be working on other things. The problem is every time a new graphics window is produced, it steals the focus, redirecting keyboard input away from what i am doing. Is there a way in r to prevent focus stealing when a graphical plot is produced?
I have searched everywhere but failed to find any reference to this. I am working in linux.
Any help greatly appreciated.
Thanks
Only on Windows: try putting a bringToTop(-1) in your function:
z <- function() {
plot(1:3)
bringToTop(-1)
}
z()
It will temporarily steal focus but then return it.
Another strategy on Windows:
z <- function(){
windows(restoreConsole=TRUE)
plot(1)
}
z()
I'm still thinking here...
If you are more interested in doing something else while the plots are being produced then I would suggest opening a pdf device so that all the plots go to a pdf file in the background and do not interfere with whatever else you are doing. Then when you are ready to look through the plots you just open the pdf file and look at the plots (and you can easily go back to previous plots this way).
If wmctrl is installed on your system, you can avoid losing focus by redefining the plot function like this:
plot <- function(...) {
graphics::plot(...)
system("wmctrl -a :ACTIVE:")
}
It seems to work quite well, in the fluxbox window manager at least. I tried different scenarios like switching to a different window during a long calculation before plot is called, and opening multiple plots.
Put it into your .Rprofile if you want it to persist.
Recently, when I wanted to display any image via X11, I just see a big white window and nothing else. For example:
#Running X11() or not doesn't matter
#X11()
#plot anything
plot(1:10)
And then, I get nothing but a white windows just like I purely run X11(). When I use other device (pdf(), png()), I can get image after dev.off().
Problem solved. It's a problem about cairo. AddgrDevices::X11.options(type="ncairo") to ~/.Rprofile
Having a blank window is the correct behaviour for calling X11(). Usually, you won't need to call that function, but it means that you can specify how tall/wide the plot window is before you create a plot.
If you still have a blank window after you try and plot something, then you are probably writing to a different device.
Have you opened another device (maybe with png, etc.) and forgotten to close it?
What does dev.cur() return?
A reproducible example of this:
png("foo.png")
x11()
dev.set(dev.list()[names(dev.list()) == "png:foo.png"])
plot(1:10)
#Make sure you call this afterwards
graphics.off()
I generate a dendrogam using a collection of r commands. It worked just fine and saved the generated dendromgram into a PDF file. To improve efficiency, I wrapped these commands as a function, which does not change anything. However, the pdf is just a blank file without any graphical content. Please let me know what’s wrong with my function defintion. Thanks.
myplot<-function(inputcsv, outputfile){
library(ggdendro)
library(ggplot2)
x<-read.csv(inputcsv,header=TRUE)
d<-as.dist(x,diag=FALSE,upper=FALSE)
hc<-hclust(d,"ave")
dhc<-as.dendrogram(hc)
ddata<-dendro_data(dhc,type="rectangle")
ddata$labels$text <- gsub("\\."," ",ddata$labels$text)
ggplot(segment(ddata))+geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1))
pdf(outputfile, width=30,height=35)
last_plot()
dev.off()
}
R FAQ
Wrap your ggplot call in a print() function.
ggplot and friends return an object, and the plotting only happens when the object is printed. When you do this on the command line the printing happens automatically. When you stick it in a script or function you have to do it yourself.
The debate on whether this is a good idea or a dumb thing that just generates questions like this continues...