如何使执行暂停,睡眠,在R中等待X秒?

如何将R脚本暂停指定的秒数或毫秒数?在许多语言中,都有一个sleep函数,但是?sleep引用一个数据集。而?pause?wait不存在。

预期的目的是用于自定时动画。所需的解决方案无需用户输入即可工作。


小唯快跑啊
浏览 1923回答 2
2回答

阿晨1998

请参阅help(Sys.sleep)。例如,来自 ?Sys.sleeptestit <- function(x){&nbsp; &nbsp; p1 <- proc.time()&nbsp; &nbsp; Sys.sleep(x)&nbsp; &nbsp; proc.time() - p1 # The cpu usage should be negligible}testit(3.7)屈服> testit(3.7)&nbsp; &nbsp;user&nbsp; system elapsed&nbsp;&nbsp; 0.000&nbsp; &nbsp;0.000&nbsp; &nbsp;3.704&nbsp;

精慕HU

如果CPU使用率很高,则Sys.sleep()将不起作用。就像其他关键的高优先级进程正在运行(并行)一样。这段代码对我有用。在这里,我以2.5秒的间隔打印1到1000。for (i in 1:1000){&nbsp; print(i)&nbsp; date_time<-Sys.time()&nbsp; while((as.numeric(Sys.time()) - as.numeric(date_time))<2.5){} #dummy while loop}
打开App,查看更多内容
随时随地看视频慕课网APP