Sys.sleep, Sys.time

Pause activity using the Sys.sleep function, or obtain the current time using Sys.time.
  • time – The number of seconds to pause R activity.
  • no arguments

Example. The example has three components. (1) Print the current time, and save the time in tStart. (2) Run a loop for 50 iterations, and in each iteration put the system to sleep for 0.2 seconds. (3) Prints out the time after the loop has completed. Notice that objects from Sys.time support subtraction, making it is easy to compute elapsed time.
> Sys.time()
[1] "2012-01-08 09:13:07 EST"
> tStart <- Sys.time()
> for(i in 1:50){
+   Sys.sleep(0.2)
+ }
> Sys.time()
[1] "2012-01-08 09:13:18 EST"
> Sys.time() - tStart
Time difference of 10.13775 secs
Tip 1. Use the function Sys.sleep to assist in reviewing output that needs only a brief review. For instance, if there are 100 plots that require a brief and somewhat superficial review, you might put a Sys.sleep(5) command after each plot command in a set of code, which would allow for a 5 second review while requiring no additional actions to move to the next plot.
Tip 2. If a Sys.sleep command is taking too long, you may hit escape. However, some caution is necessary since doing so will also escape from a loop.

Leave a Reply