Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

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
edited by

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).

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...