在 R 命令行中显示时钟
我想知道是否有一种方法可以在 R 命令行中显示当前时间,就像在 MS DOS 中一样,我们可以使用
Prompt $T $P$G
在每个提示行中包含时钟。 类似的事情
options(prompt=paste(format(Sys.time(), "%H:%M:%S"),"> "))
会做到这一点,但随后它在设置时就被修复了。我不知道如何让它自动更新。
I wonder if there is a way to display the current time in the R command line, like in MS DOS, we can use
Prompt $T $P$G
to include the time clock in every prompt line.
Something like
options(prompt=paste(format(Sys.time(), "%H:%M:%S"),"> "))
will do it, but then it is fixed at the time it was set. I'm not sure how to make it update automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Chase 指出了正确的方法,因为
options("prompt"=...)
可以用于此目的。但他的解决方案添加了一个常数时间表达式,这不是我们想要的。函数
taskCallbackManager
的文档包含其余内容:我们注册一个回调,该回调在每个命令完成后进行评估。这就是窍门。更精彩的文档位于 R 开发者网站的本文档。
Chase points the right way as
options("prompt"=...)
can be used for this. But his solutions adds a constant time expression which is not what we want.The documentation for the function
taskCallbackManager
has the rest:We register a callback that gets evaluated after each command completes. That does the trick. More fancy documentation is in this document from the R developer site.
除非执行顶级命令,否则基于回调的其他方法都不会更新提示。因此,在控制台中按回车键不会产生更改。这就是 R 标准回调处理的本质。
如果安装 tcltk2 软件包,您可以设置一个任务调度程序来更改
option()
,如下所示:瞧,类似于 MS DOS 提示符。
注意:灵感来自这个答案。
注意 1:等待时间(本例中为 1000)指的是毫秒数,而不是秒数。当亚秒分辨率在某种程度上有用时,您可以向下调整它。
None of the other methods, which are based on callbacks, will update the prompt unless a top-level command is executed. So, pressing return in the console will not create a change. Such is the nature of R's standard callback handling.
If you install the
tcltk2
package, you can set up a task scheduler that changes theoption()
as follows:Voila, something like the MS DOS prompt.
NB: Inspiration came from this answer.
Note 1: The wait time (1000 in this case) refers to the # of milliseconds, not seconds. You might adjust it downward when sub-second resolution is somehow useful.
这是另一种回调解决方案:
这与 Dirk 的方法相同,但语法对我来说更简单一些。
Here is an alternative callback solution:
This works the same as Dirk's method, but the syntax is a bit simpler to me.
您可以通过
options()
命令更改显示的默认字符。您可能想尝试这样的操作:查看
?options
的帮助页面,获取可以设置的完整列表。这是一个非常有用的知识!假设您希望为每个 R 会话执行此操作,请考虑将其移至您的
.Rprofile
。关于该主题,可以在这里找到其他一些关于编程快乐的好东西。You can change the default character that is displayed through the
options()
command. You may want to try something like this:Check out the help page for
?options
for a full list of things you can set. It is a very useful thing to know about!Assuming this is something you want to do for every R session, consider moving that to your
.Rprofile
. Several other good nuggets of programming happiness can be found hither on that topic.我不知道 R 的本机函数可以执行此操作,但我知道 R 与其他具有系统时间命令的语言有接口。也许这是一个选择?
Thierry 提到了
system.time()
,还有proc.time()
,具体取决于您需要它的用途,尽管它们都不能提供当前时间。I don't know of a native R function for doing this, but I know R has interfaces with other languages that do have system time commands. Maybe this is an option?
Thierry mentioned
system.time()
and there is alsoproc.time()
depending on what you need it for, although neither of these give you the current time.