MyNixOS website logo
Description

A Flexible and Robust Sys.sleep() Replacement.

Provides a near drop-in replacement for base::Sys.sleep() that allows more types of input to produce delays in the execution of code and can silence/prevent typical sources of error.

License Travis-CI Build Status CRAN_Status_Badge codecov.io

Why should I use it?

naptime makes delaying code execution in R more flexible and robust. It makes delaying code execution more flexible by supporting more data types than base::Sys.sleep(). It makes delaying code more robust by allowing errors in the delay specification to throw warnings instead of errors. Most notably, because naptime supports more input data types, you can use package:lubridate functions in conjunction with naptime to yield human readable time delays and intervals.

Consider the case of waiting one hour:

Sys.sleep(3600)

# versus

naptime(lubridate::hours(1))

Consider the case of wanting to start processing every hour for a job of an indeterminate duration:

repeat{
  start_time <- lubridate::now()
  # Do processing
  sleep_duration <- 3600 - as.numeric(lubridate::now() - start_time)
  if (sleep_duration > 0) {
    Sys.sleep(sleep_duration)
  }
}

# versus

repeat{
  start_time <- lubridate::now()
  # Do processing
  naptime(start_time + lubridate::hours(1))
}

How do I use it?

Because naptime() has nearly identical arguments and behavior as base::Sys.sleep() in response to numeric inputs, it can be nearly used as a drop-in replacement for base::Sys.sleep().

There are two notable differences in the behavior of Sys.sleep() and naptime():

  • For the input value of Infbase::Sys.sleep() will sleep indefinitely. In contrast, naptime::naptime() will produce an error (or if naptime.permissive = TRUE is set pause the default duration).
  • For a negative input value base::Sys.sleep() will produce an error. In contrast, naptime::naptime() will assume that the period of delay has already elapsed and move forward without further delay.

Options

All options are set via base::options().

  • naptime.default_delay. If left unchanged, the default delay is .1 seconds.
  • naptime.warnings. If left unchanged, the default is TRUE, to show warnings.
  • naptime.permissive. If left unchanged, the default is FALSE, to trigger errors on inputs that couldn't be converted into something sensible. If set TRUE, inputs that couldn't be converted into something sensible will result in a default delay.

Polymorphic inputs

naptime() accepts a wide variety of inputs.

Polymorphism for:

  • numeric: time in seconds to nap
naptime(1)
#> NULL
  • POSIXct: time at which the nap should stop (timezone is respected)
naptime(lubridate::now(tzone = "UTC")+lubridate::seconds(1))
#> NULL
  • Period: time from now at which the nap should stop
naptime(lubridate::seconds(1))
#> NULL
  • character: Date or date time at which nap should stop formatted as yyyy-mm-dd hh:mm:ss, time zone is assumed to be Sys.timezone() and hh:mm:ss is optional as three formats may be missing, cf. lubridate::ymd_hms()..
naptime(as.character(lubridate::now() + lubridate::seconds(1)))
#> NULL
  • difftime: difference in time to nap
naptime(difftime(lubridate::now() + lubridate::seconds(1), lubridate::now()))
#> NULL
  • logical: nap for default duration if TRUE, skip nap if FALSE
naptime(TRUE)
#> NULL
  • NULL; meaning no delay
naptime(NULL)
#> NULL
  • generic: By default this produces an error, however, you can set naptime.permissive as an option (or argument) that will cause this to nap for default duration instead.
naptime(glm(rnorm(5) ~ runif(5)), permissive = TRUE)
#> Warning: The time paramater was not scalar (length equal to 1)
#> Warning: unhandled input for naptime(): Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'naptime' for signature '"list"'
#> NULL
options(naptime.permissive = TRUE)
naptime(glm(rnorm(5) ~ runif(5)))
#> Warning: The time paramater was not scalar (length equal to 1)

#> Warning: unhandled input for naptime(): Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'naptime' for signature '"list"'
#> NULL

If you find a reasonable input-type for which naptime::naptime() doesn't have a reasonable response, please file an issue or PR in which you resolve the shortcoming.

How do I get it?

The current version is on CRAN, but you can fetch an early release of the upcoming build directly from github:

library(devtools)
install_github("drknexus/naptime")
library(naptime)

Author's Note

The initial draft of this code was written by Timothy Gann under a spec drafted by Russell Pierce. Many improvements and bug fixes to the original code, all packaging, and all tests were written by Russell Pierce. Russell Pierce is the current maintainer and responsible party for this package.

Metadata

Version

1.3.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows