Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R script using X11 window only opens for a second

Tags:

r

x11

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!

like image 602
pikatma Avatar asked Jun 14 '14 14:06

pikatma


1 Answers

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...

like image 187
Dirk Eddelbuettel Avatar answered Oct 14 '22 07:10

Dirk Eddelbuettel