如何让执行暂停,休眠,等待X秒在R?

时间:2023-01-15 00:09:41

How do you pause an R script for a specified number of seconds or miliseconds? In many languages, there is a sleep function, but ?sleep references a data set. And ?pause and ?wait don't exist.

你如何暂停一个R脚本的指定的秒或毫秒?在许多语言中,有一个睡眠功能,但是?睡眠引用一个数据集。暂停和?等待不存在。

The intended purpose is for self-timed animations. The desired solution works without asking for user input.

预期的目的是为自计时的动画。想要的解决方案可以在不需要用户输入的情况下工作。

3 个解决方案

#1


112  

See help(Sys.sleep).

看到帮助(Sys.sleep)。

For example, from ?Sys.sleep

例如,来自哪里? Sys.sleep

testit <- function(x)
{
    p1 <- proc.time()
    Sys.sleep(x)
    proc.time() - p1 # The cpu usage should be negligible
}
testit(3.7)

Yielding

屈服

> testit(3.7)
   user  system elapsed 
  0.000   0.000   3.704 

#2


1  

Sys.sleep() will not work if the CPU usage is very high; as in other critical high priority processes are running (in parallel).

如果CPU使用率很高,则sleep()将不起作用;在其他关键的高优先级进程中(并行运行)。

This code worked for me. Here I am printing 1 to 1000 at a 2.5 second interval.

这段代码对我有效。在这里我打印1到1000在2。5秒的间隔。

for (i in 1:1000)
{
  print(i)
  date_time<-Sys.time()
  while((as.numeric(Sys.time()) - as.numeric(date_time))<2.5){} #dummy while loop
}

#3


0  

I made an example for you, I hope it is useful

我给你做了个例子,希望它有用。

# 1st let make a graph with limits xlim =0:10 e ylim=0:10.
plot(0:10,0:10, type="n")

# let use the 'for' to put texts on graph:
for(i in 1:10)
    text(i,i, paste("**", i))

## let retard steps 1 sec

plot(0:10,0:10, type="n")
for(i in 1:9){
    text(i,i, paste("step", i))
    Sys.sleep(1) 
}

# please wait some seconds.......
# now faster
plot(0:10,0:10, type="n")
for(k in 1:9){
    text(k,k, paste("step", k))
    Sys.sleep(.1) ## retard steps 0,1 sec
}

#1


112  

See help(Sys.sleep).

看到帮助(Sys.sleep)。

For example, from ?Sys.sleep

例如,来自哪里? Sys.sleep

testit <- function(x)
{
    p1 <- proc.time()
    Sys.sleep(x)
    proc.time() - p1 # The cpu usage should be negligible
}
testit(3.7)

Yielding

屈服

> testit(3.7)
   user  system elapsed 
  0.000   0.000   3.704 

#2


1  

Sys.sleep() will not work if the CPU usage is very high; as in other critical high priority processes are running (in parallel).

如果CPU使用率很高,则sleep()将不起作用;在其他关键的高优先级进程中(并行运行)。

This code worked for me. Here I am printing 1 to 1000 at a 2.5 second interval.

这段代码对我有效。在这里我打印1到1000在2。5秒的间隔。

for (i in 1:1000)
{
  print(i)
  date_time<-Sys.time()
  while((as.numeric(Sys.time()) - as.numeric(date_time))<2.5){} #dummy while loop
}

#3


0  

I made an example for you, I hope it is useful

我给你做了个例子,希望它有用。

# 1st let make a graph with limits xlim =0:10 e ylim=0:10.
plot(0:10,0:10, type="n")

# let use the 'for' to put texts on graph:
for(i in 1:10)
    text(i,i, paste("**", i))

## let retard steps 1 sec

plot(0:10,0:10, type="n")
for(i in 1:9){
    text(i,i, paste("step", i))
    Sys.sleep(1) 
}

# please wait some seconds.......
# now faster
plot(0:10,0:10, type="n")
for(k in 1:9){
    text(k,k, paste("step", k))
    Sys.sleep(.1) ## retard steps 0,1 sec
}