pikatma
pikatma

Reputation: 63

R script using X11 window only opens for a second

I'm running an R script via my Linux Mint 16 command line. It contains a boxplot that I want to display in a window. So I'm using x11() function for creating that window. Here is my code:

testdata <- data.frame(sample(1:1000,size=100,replace=T), row.names=NULL)
colnames(testdata)<-c("data")

x11()
boxplot(testdata, main="Boxplot der Testdaten", horizontal=TRUE)

When I run this function in Rstudio, it will open a window and show the boxplot created. But whenever I run it from the command line of my Linux Mint 16 machine, the window will open for a second and then close again. I can see the boxplot for a second. I couldn't really find a reason for this. I'm quite new to R and never used X11 before. Any ideas would be really appreciated. Thanks!

Upvotes: 6

Views: 3270

Answers (2)

Will
Will

Reputation: 1313

You can sleep until all windows are closed

while(names(dev.cur()) !='null device') Sys.sleep(1)

on my machine, after calling x11(), names(dev.cur()) is "X11cairo". After closing all/any windows opened with x11, names(dev.cur()) becomes "null device"

testdata <- data.frame(sample(1:1000,size=100,replace=T), row.names=NULL)
colnames(testdata)<-c("data")

x11()
boxplot(testdata, main="Boxplot der Testdaten", horizontal=TRUE)
# wait until window is closed (check every second)
while(names(dev.cur()) !='null device') Sys.sleep(1)

Upvotes: 4

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

This is more-or-less a FAQ. Part of this is that you seem to misunderstand how all commands terminate. I.e. when you call ls it does not stop either.

So here you need to something extra. Possibly approaches:

  • Just sleep via Sys.sleep(10) which would wait ten seconds.

  • Wait for user input via readLines(stdin()) or something like that [untested]

  • Use the tcltk package which comes with R and is available on all platforms to pop up a window the user has to click to make the click disappear. That solution has been posted a few times over the years on r-help.

But in this day and age, you may also rethink the issue. I had good success preparing analysis and visualization for colleagues via the most-awesome shiny package which displays to a web page. Everybody has a web browser...

Upvotes: 6

Related Questions