bash 运行带有绘图的 R 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3301694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running R Scripts with Plots
提问by Stephen Diehl
I have a small shell script (bash) which runs a R script which produces a plot as output. Everything works fine but immedietly after the plot is rendered R quits. Is there a way to keep the R session alive until the plot window is closed.
我有一个小的 shell 脚本 (bash),它运行一个 R 脚本,该脚本生成一个绘图作为输出。一切正常,但在情节渲染后立即退出 R。有没有办法让 R 会话保持活动状态,直到绘图窗口关闭。
The shell script.
外壳脚本。
#!/bin/bash
R --slave --vanilla < myscript.r
And the R script.
和 R 脚本。
daq = read.table(file('mydata.dat'))
X11()
pairs(daq)
//R Completes this and then exits immediately.
Thanks in advance for any help!
在此先感谢您的帮助!
采纳答案by Mark
If you use the Rscript command (which is better suited for this purpose), you run it like this:
如果你使用 Rscript 命令(它更适合这个目的),你可以像这样运行它:
#!/usr/bin/Rscript
daq = read.table(file('mydata.dat'))
X11()
pairs(daq)
message("Press Return To Continue")
invisible(readLines("stdin", n=1))
Make sure to set the execute permission on myscript.r, then run like:
确保在 myscript.r 上设置执行权限,然后像这样运行:
/path/to/myscript.r
or without the shebang:
或没有shebang:
Rscript /path/to/myscript.r
回答by Alex Deckmyn
You could add a loop that checks for the graphical device every n seconds:
您可以添加一个循环,每 n 秒检查一次图形设备:
while (!is.null(dev.list())) Sys.sleep(1)
This will sleep until you close the plot window.
这将休眠,直到您关闭绘图窗口。
回答by mbq
This is not a perfect solution, but you may call locator()
just after the plot command.
Or just save the plot to pdf and then invoke pdf viewer on it using system
.
这不是一个完美的解决方案,但您可以locator()
在 plot 命令之后调用。
或者只是将绘图保存为 pdf,然后使用system
.
回答by chrisamiller
One solution would be to write the plot out to pdf instead:
一种解决方案是将绘图写成 pdf:
pdf(file="myplot.pdf")
##your plot command here
plot( . . . )
dev.off()
回答by aL3xa
More important question is why do you want R to run after graph creation? Use it either in interactive mode or in batch mode... I don't understand what do you want to accomplish. Besides, try littler
, it's located in Ubuntu repos (universe repos, if I'm correct), or Rscript
, so rewrite your script and name it myscript.r, and be sure to put correct path in the first line. Try whereis Rscript
(usually /usr/bin/Rscript). Forget about bash script. You can pass --vanilla and --slave arguments to Rscript, but I don't see the purpose... O_o
更重要的问题是为什么要在创建图形后运行 R?在交互模式或批处理模式下使用它......我不明白你想完成什么。此外,尝试littler
,它位于 Ubuntu 存储库中(如果我是正确的,则为宇宙存储库),或者Rscript
,因此重写您的脚本并将其命名为 myscript.r,并确保在第一行中放置正确的路径。试试whereis Rscript
(通常是/usr/bin/Rscript)。忘记 bash 脚本。您可以将 --vanilla 和 --slave 参数传递给 Rscript,但我看不出目的... O_o