Placement of various values ​​for Sys.sleep in a loop, several time values

I have a code and I use a loop inside it, now I want this loop to be stopped for a short time after the completion of one request. But when this loop executes the next request, I want it to be stopped at a different time interval. For example, for After the first request - 10 seconds. After the second request - 15 seconds, etc.

Below is a loop

for (i in 1:nrow(uniquemail)) {
    k <- 2
    test3 <- subset(finaltest, finaltest$Email == uniquemail[i,1])
    for (j in 1:nrow(test3)) {

      uniquemail[i,k] <- test3[j,2]
      uniquemail[i,k+1] <- as.character(test3[j,3])
      uniquemail[i,k+2] <- as.numeric(test3[j,4])
      uniquemail[i,k+3] <- as.numeric(test3[j,5])

      k <- k+4

      print(paste(i,j,k)) 
    }
  }

Is there any way to do this. I used Sys.sleep, but don’t know how to use it for my motive above.

+4
source share
2 answers

, 5 , : . :

for (j in 1:3) {
tm <- proc.time() #take timing before your action

#do your things here
#
#

tmdiff <-(5 * j) - (proc.time()[3] - tm[3]) #you want to wait 5 seconds the first time,
#10 sec the second time, 15 sec the third time etc..

if (tmdiff > 0) {
  #depending of how long your "thing" took,
  print(paste("sleep for", tmdiff, "seconds"))#you are taking the difference from the start to end of "actions" and sleeping for that time
  Sys.sleep(tmdiff)
  }
}

:

for (j in 1:3) {    
    #do your things here
    #
    #  
      tmsleep<-(5 * j) #you want to wait 5 seconds the first time,
                       #10 sec the second time, 15 sec the third time etc..
      print(paste("sleep for", tmsleep, "seconds"))
      Sys.sleep(tmsleep)
    }
  }

( ), , - API / -. , , :

tmsleep<-sample(5:15,1)
Sys.sleep(tmsleep)

. 5 15 . , , set.seed(j), j - :

set.seed(j)
tmsleep<-sample(5:15,1)
Sys.sleep(tmsleep)
+2

", , ?"

: , :

timings <- c(5, 2, 10, 8, 7)

, , , :

timings <- sample(5:10, 10)

Sys.sleep . , , .

Sys.sleep(timings[(i-1) %% length(timings) + 1])

:

for (i in 1:15){      
  print(timings[(i-1) %% length(timings) + 1])
}

:

[1] 5
[1] 2
[1] 10
[1] 8
[1] 7
[1] 5
[1] 2
[1] 10
[1] 8
[1] 7
[1] 5
[1] 2
[1] 10
[1] 8
[1] 7
0

Source: https://habr.com/ru/post/1652951/


All Articles