Fail to display any image via X11 - r

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()

Related

png, pdf, tiff margin limitations

In order to get some space for a special ylab text, I use mar=c(5,7,4,2). This provides me with 7 lines of space for ylab. On the default device (screen) everything functions as anticipated. However, I cannot get this output to any other device than the screen.
par(mar=c(5,7,4,2))
png(file="a.png", width=500, height=500)
plot(1,1,ylab="A very very long axis title\nthat need special care",xlab="",type="n")
I verified the same behavior with png, tiff, pdf. It seems that the maximum printable size in these devices is 4. Anything that goes beyond this number gets cut off. The same behavior is when plotting xlabs, eg by using mgp=c(5,1,0). mgp=c(4,1,0) (line 4) is the maximum printable line in any other device than the screen.
Even after upgrading to the latest R version does not change this behavior and it is the same on Windows and Ubuntu.
Any advice on the root cause of this behavior is appreciated.
The problem is the order of your statements. The par() call applies to the current device. Since you open the png() device after that, it doesn't have any effect. Just put things in this order and they'll be fine:
png(file="a.png", width=500, height=500)
par(mar=c(5,7,4,2))
plot(1,1,ylab="A very very long axis title\nthat need special care",xlab="",type="n")
dev.off()
This gives this image in the file:

display a png picture and hold it for 3 seconds in R

I am very new to user interface using R. I have a png file. I would like to display it at the beginning of my script, keep it to stay for around 3 seconds and close it automatically.
I tried file.show(). R will pop the png file. But that is not what I want.
I prefer to make it appear in front of my IDE like a splash screen that appears before a program starts. I mean it will be similar to a welcome pic before the user really start to run the script. After 3 seconds, it will disappear and R will start to run the script automatically.
I wonder any R package can make my idea come true? Thank you.
This probably could get you started. The image will appear in the default RStudio bottom-right panel for 3 seconds and then will disappear. Warning: dev.off() will erase all your plots that you had in workspace before running the script.
#At the very beginning of the script, execute:
library(png)
img <- readPNG("path/baboon.png")
windows()
grid::grid.raster(img)
Sys.sleep(3)
dev.off()
#... your script continues here
print("Did you see that baboon??")
Edit: windows() does exactly what you need, I think

Code to clear all plots in RStudio

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:

Inhibit focus stealing when launching a new graphics plot in r

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.

Using jpeg(file= ) creates an empty jpeg even though there's not a call to create a plot

This is observed running in batch. Given the following code snippet, I don't understand why R creates an empty jpeg file in Windows even though I am not calling a plot or graph. When I run similar code under Linux or OS X, the jpeg file is not created. I don't know ahead of time if the user is going to want a plot so I setup the filename(s) ahead of time and give them a name and location.
##
sink("c:\\temp\\test.lst")
jpeg(file="c:\\temp\\test%d.jpeg")
norm <- rnorm(100)
print(norm)
Any suggestions would be appreciated.
The ?jpeg help file (which also applies to bmp(), png() and tiff() devices) indicates that:
The ‘type = "windows"’ versions of these devices effectively plot
on a hidden screen and then copy the image to the required format.
This Windows-specific implementation detail most likely explains the difference in the behavior of Windows and *NIX systems.
On Windows, calling any of the functions above (and pdf() and postscript() as well) creates a file --- whether or not you then plot anything to that hidden screen. Except for pdf() (which produces files that I can't open with a viewer), the image that's registered on the plotting device is that of a white rectangle of height and width specified in the call to the specific device.
I wouldn't worry about it. if the prospect of zero-length files cluttering up the folder bothers you, you can clean up afterwards with something like
jpgs <- file.path("c:/temp", dir(pattern="c:/temp/test[0-9]*\\.jpeg"))
s <- file.info(jpgs)[["size"]]
for(i in seq_along(s))
if(s[i] == 0) file.remove(jpgs[i])

Resources