0 votes
1 view
in R Programming by (5.2k points)

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

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

1 Answer

0 votes
by (25.3k points)

You can use the Sys.sleep function from the base package to suspend execution for a given time interval.

According to R Documentation:

Using this function allows R to temporarily be given very low priority and hence not to interfere with more important foreground tasks. Typical use is to allow a process launched from R to set itself up and read its input files before R execution is resumed.

For example:

testit <- function(x)

{

    p1 <- proc.time()

    Sys.sleep(x)

    proc.time() - p1 # The cpu usage should be negligible

}

testit(3.7)

 testit(3.7)

   user  system elapsed 

   0.00    0.00    3.71 

testit(5)

   user  system elapsed 

   0.01    0.00    5.05 

To know more about this function see help(Sys.sleep).

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...