R script using X11 window only opens for a second

馋奶兔 提交于 2019-12-12 11:09:48

问题


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!


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/24220676/r-script-using-x11-window-only-opens-for-a-second

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!