在 bash 脚本中运行时,使 R(统计包)等待键盘提示 [英] Make R (statistics package) wait for keyboard prompt when run within a bash script

查看:35
本文介绍了在 bash 脚本中运行时,使 R(统计包)等待键盘提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 R 在循环中生成一系列绘图,用户按 Enter 键表示他们已经看到了绘图,是时候继续前进了.这些是使用 rgl 包生成的交互式可旋转图,因此使用 Sys.sleep() 之类的东西还不够好.

I am using R to generate a series of plots within a loop, with the user hitting the enter key to indicate they have seen the plot and it is time to move on. These are interactive rotatable plots generated with the rgl package and therefore using something like Sys.sleep() is not good enough.

目前我可以使用 readline(),它可以在交互式运行 R 时找到.但是,如果我想在 bash 脚本中运行我的 R 脚本,所有图都会在屏幕前闪烁.无论我使用以下方法调用 R 都会发生这种情况:

Currently I can use readline() which works find when running R interactively. However, if I want to run my R script within a bash script all the plots appear flashing before the screen. This happens whether I call R using:

R --no-save -f myfile.r
R --no-save -e "source('myfile.r')"
R --no-save << myfile.r

如何让 R 在作为 bash 子进程运行时暂停并等待用户点击?

How do I get R to pause and wait for the user to hit when run as a bash subprocess?

推荐答案

这是一个迟到的答案,但我的目标是相似的:Rscript 执行应该调出一个带有情节的 rgl 窗口,而不是其他任何东西,并且它应该保持在那里直到窗口已关闭,即 rgl 窗口不应终止.

It's a late answer but my goal was similar: Rscript execution should bring up an rgl window with a plot and nothing else, and it should remain there till the window is closed, i.e. the rgl window should not terminate.

为了实现这一点,我只是将它放在 R 脚本的末尾,rgl 图将保留在那里进行操作,直到您退出窗口,消耗很少的 CPU 时间:

To achieve this, I simply placed this at the end of the R script, and the rgl plot will remain there for manipulation until you quit the window, consuming little CPU time:

play3d(function(time) {Sys.sleep(0.01); list()} )

对于常规的 2D R 绘图,locator() 的工作原理类似,或者 locator(1)(如果单击应关闭绘图窗口).

For regular 2D R plots, locator() works similarly, or locator(1) if one click should close the plot window.

这篇关于在 bash 脚本中运行时,使 R(统计包)等待键盘提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆