Home > Software engineering >  Sys.sleep with runif, set and use variable
Sys.sleep with runif, set and use variable

Time:03-17

I just want to create a variable I can set to certain values to use it afterwards with runif for Sys.sleep function (for different functions):

sys_sleep_var = c(0.2, 0.4)
Sys.sleep(runif(1, sys_sleep_var))

This doesn't work, it seems to be easy to fix, but I dont know how. Thanks in advance!

The result should be this:

Sys.sleep(runif(1,0.2,0.4))

CodePudding user response:

It goes wrong in your runif()

You need to provide a min and max, otherwise it is between 0-1

runif(n = 1, min = 0.2, max = 0.4)

What you basically do is provide a vector as value for min which picks the first value, so you range between 0.2 and 1

runif(n = 1, min = c(0.2, 0.4))

You can solve it using this:

runif(1, sys_sleep_var[1], sys_sleep_var[2])

CodePudding user response:

If you want to pass a bunch of variables as arguments to a function, put them in a list and use do.call:

sys_sleep_var = list(1, 0.2, 0.4)
Sys.sleep(do.call(runif, sys_sleep_var))
  • Related