使用 X11 窗口的 R 脚本只打开一秒钟 [英] R script using X11 window only opens for a second
问题描述
我正在通过 Linux Mint 16 命令行运行 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)
当我在 Rstudio 中运行这个函数时,它会打开一个窗口并显示创建的箱线图.但是每当我从 Linux Mint 16 机器的命令行运行它时,窗口都会打开一秒钟然后再次关闭.我可以看到一秒钟的箱线图.我实在找不到原因.我对 R 很陌生,以前从未使用过 X11.任何想法将不胜感激.谢谢!
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!
推荐答案
这或多或少是一个常见问题解答.部分原因是您似乎误解了所有命令的终止方式.IE.当您调用 ls
时,它也不会停止.
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:
只需通过
Sys.sleep(10)
睡眠,它会等待十秒钟.
Just sleep via
Sys.sleep(10)
which would wait ten seconds.
通过 readLines(stdin())
或类似的东西等待用户输入 [未经测试]
Wait for user input via readLines(stdin())
or something like that [untested]
使用 R 自带的 tcltk 包,可在所有平台上弹出一个窗口,用户必须点击才能使点击消失.多年来,该解决方案已在 r-help 上发布数次.
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.
但在这个时代,你也可以重新思考这个问题.我通过最棒的 shiny 包为同事准备分析和可视化取得了成功,该包显示在网页.每个人都有一个网络浏览器...
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...
这篇关于使用 X11 窗口的 R 脚本只打开一秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!