Skip to content

Commit

Permalink
Merge pull request #3 from drknexus/develop
Browse files Browse the repository at this point in the history
Updating Master to Develop for CRAN release
  • Loading branch information
russellpierce authored Nov 13, 2016
2 parents 6cb1f56 + ec4239f commit 7a9dfd1
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
^\.Rproj\.user$
^\.travis\.yml$
^revdep$
cran-comments.md
^README\.Rmd$
^README-.*\.png$
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ language: R
sudo: false
cache: packages

language: r

before_install:
- export TZ=Europe/Vienna

#matrix: 3x Linux, 1x OS X
matrix:
include:
- r: release
- r: oldrel
- r: devel
- r: 3.3.2
- os: osx
osx_image: xcode7.2
r: release
latex: false

r_github_packages:
- jimhester/covr

Expand Down
14 changes: 8 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: naptime
Type: Package
Title: A Robust Flexible Sys.sleep Replacement
Version: 0.1.2.0000
Date: 2016-07-09
Title: A Robust Flexible Sys.sleep() Replacement
Version: 1.0.0
Date: 2016-11-12
Authors@R: c(
person("Timothy", "Gann", role = c("aut"), email = "timothy.gann@gmail.com"),
person("Russell", "Pierce", role = c("aut", "cre"), email = "russell.pierce@zapier.com")
person("Russell", "Pierce", role = c("aut", "cre"), email = "russell.s.pierce@gmail.com"),
person("Timothy", "Gann", role = c("aut"), email = "timothy.gann@gmail.com")
)
Description: Provides a near drop-in replacement for Sys.sleep that allows more types of input
Description: 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 silences/prevents typical sources of error.
URL: https://github.com/drknexus/naptime
BugReports: https://github.com/drknexus/naptime/issues
Expand All @@ -22,3 +22,5 @@ Suggests:
rmarkdown
RoxygenNote: 5.0.1
VignetteBuilder: knitr
URL: https://github.com/drknexus/naptime
BugReports: https://github.com/drknexus/naptime/issues
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(naptime)
importFrom(lubridate,now)
importFrom(lubridate,period_to_seconds)
importFrom(lubridate,seconds)
importFrom(lubridate,tz)
importFrom(lubridate,ymd)
importFrom(lubridate,ymd_hms)
importFrom(methods,new)
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# naptime 1.0.0

* Initial CRAN Submission
* 100% Code Coverage
* Polymorphism for:
* numeric
* NULL
* POSIXct
* Period
* character
* difftime
* logical

23 changes: 14 additions & 9 deletions R/naptime.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
#'
#' Acceptable inputs:
#' \itemize{
#' \item \code{numeric} Time in seconds
#' \item \code{Period} Any positive time difference, converts dynamically to seconds
#' \item numeric: time in seconds to nap
#' \item NULL: nap for default duration
#' \item POSIXct: time at which the nap should stop (timezone is respected)
#' \item Period: time from now at which the nap should stop
#' \item character: yyyy-mm-dd hh:mm:ss at which nap should stop, time zone is assumed to be Sys.timezone() and hh:mm:ss is optional as three formats may be missing, cf. lubridate::ymd_hms().
#' \item difftime: difference in time to nap
#' \item logical: nap for default duration
#' \item generic: nap for default duration
#' }
#' @param time Time to sleep, polymorphic type inputs, leaning towards units as 'seconds'
#' @rdname naptime
#'
#' @return NULL; A side effect of a pause in program execution
#' @importFrom lubridate period_to_seconds ymd_hms ymd seconds now tz
#' @importFrom methods new
#' @export
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -59,7 +67,7 @@ setMethod("naptime", signature("Period"),
setMethod("naptime", signature("POSIXct"),
function(time)
{
t <- as.numeric(time) - as.numeric(lubridate::now(lubridate::tz(time)))
t <- as.numeric(time) - as.numeric(lubridate::now(tzone = lubridate::tz(time)))
naptime(t)
})

Expand Down Expand Up @@ -95,17 +103,14 @@ setMethod("naptime", signature("NULL"),
setMethod("naptime", signature("character"),
function(time)
{
if (nchar(time) >= 19) {
time_parsed <- try(lubridate::ymd_hms(time), silent = TRUE)
} else {
#undocumented functionality
time_parsed <- suppressWarnings(try(as.POSIXlt(lubridate::ymd(time)), silent = TRUE))
time_zone <- ifelse(is.na(Sys.timezone()), "UTC", Sys.timezone())
if (nchar(time) >= 8) {
time_parsed <- try(lubridate::ymd_hms(time, tz = time_zone, truncated = 3), silent = TRUE)
}
if ("try-error" %in% class(time_parsed) || is.na(time_parsed)) {
nap_warn("Could not parse ", time, " as time, sleeping for .Options$naptime.default_delay seconds.")
t <- nap_default()
} else {
# we don't actually respect timezones in the current version, everything is assumed to be in UTC
t <- time_parsed - lubridate::now(tzone = lubridate::tz(time_parsed))
}
naptime(t)
Expand Down
92 changes: 92 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE, results='hide', message=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
library(naptime)
library(lubridate)
```
# naptime: A Robust Flexible Sys.sleep() Replacement
[![License](http://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-2.0.html)
[![Travis-CI Build Status](https://travis-ci.org/drknexus/naptime.svg?branch=master)](https://travis-ci.org/drknexus/naptime)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/naptime)](https://cran.r-project.org/package=naptime)
[![codecov.io](https://codecov.io/github/drknexus/naptime/coverage.svg?branch=master)](https://codecov.io/github/drknexus/naptime?branch=master)

# Why should I use it?

The premise of naptime is two fold.

1. Delays in R code should never result in an error.
2. Functions should accept multiple inputs types, but result in a single output type.

In response to these considerations `naptime::naptime()` either sleeps for the specified duration, or passes forward with a warning. It has the same argument name as `base::Sys.sleep()` and a nearly identical behavior in response to numeric inputs. As such, it can be nearly used as a drop-in replacement for `base::Sys.sleep()`.

The one notable exception to identical behavior is the input value of `Inf`. `base::Sys.sleep()` will sleep indefiniately in reponse to `Inf`. In contrast, `naptime::naptime()` will sleep the default duration.

# How do I use it?

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

# Polymorphic inputs
naptime() accepts a wide variety of inputs.

Polymorphism for:
* numeric: time in seconds to nap
```{r}
naptime(1)
```
* NULL
```{r}
naptime(NULL)
```
* POSIXct: time at which the nap should stop (timezone is respected)
```{r}
naptime(lubridate::now(tzone = "UTC")+lubridate::seconds(1))
```
* Period: time from now at which the nap should stop
```{r}
naptime(lubridate::seconds(1))
```
* character: ymd_hms at which nap should stop, time zone is assumed to be the local timezone if discoverable, otherwise UTC
```{r}
naptime(as.character(lubridate::now() + lubridate::seconds(1)))
```
* difftime: difference in time to nap
```{r}
naptime(difftime(lubridate::now() + seconds(1), lubridate::now()))
```
* logical: nap for default duration
```{r}
naptime(TRUE)
```
* generic: nap for default duration
```{r}
naptime(glm(rnorm(5) ~ rnorm(5)))
```
If you find a reasonable input-type for which `naptime::naptime()` doesn't have a reasonable response, please file [an issue](https://github.com/drknexus/naptime/issues).

In addition, naptime will handle some specific errors:

* All negative intervals will be converted to a delay of 0
* All non-finite intervals will be converted to a default delay
* All inputs that produce an error will be converted to a default delay

# How do I get it?
```
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.

124 changes: 101 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,116 @@
# naptime: A Robust Flexible Sys.sleep Replacement
[![License](http://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-2.0.html)
[![Travis-CI Build Status](https://travis-ci.org/drknexus/naptime.svg?branch=master)](https://travis-ci.org/drknexus/naptime)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/naptime)](http://cran.r-project.org/package=naptime)
[![codecov.io](https://codecov.io/github/drknexus/naptime/coverage.svg?branch=master)](https://codecov.io/github/drknexus/naptime?branch=master)

The premise of naptime is two fold.
<!-- README.md is generated from README.Rmd. Please edit that file -->
naptime: A Robust Flexible Sys.sleep() Replacement
==================================================

1. Delays in R code should never result in an error.
2. Functions should accept multiple inputs types, but result in a single output type.
[![License](http://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-2.0.html) [![Travis-CI Build Status](https://travis-ci.org/drknexus/naptime.svg?branch=master)](https://travis-ci.org/drknexus/naptime) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/naptime)](https://cran.r-project.org/package=naptime) [![codecov.io](https://codecov.io/github/drknexus/naptime/coverage.svg?branch=master)](https://codecov.io/github/drknexus/naptime?branch=master)

In response to these considerations `naptime::naptime()` either sleeps for the specified duration, or passes forward with a warning. It has the same argument name as `base::Sys.sleep()` and a nearly identical behavior in response to numeric inputs. As such, it can be nearly used as a drop-in replacement for `base::Sys.sleep()`.
Why should I use it?
====================

The one notable exception to identical behavior is the input value of `Inf`. `base::Sys.sleep()` will sleep indefiniately in reponse to `Inf`. In contrast, `naptime::naptime()` will sleep the default duration.
The premise of naptime is two fold.

# Options
All options are set via base::options().
1. Delays in R code should never result in an error.
2. Functions should accept multiple inputs types, but result in a single output type.

* `naptime.default_delay`. If left unchanged, the default delay is `.1` seconds.
* `naptime.warnings`. If left unchanged, the default is `TRUE`, to show warnings.
In response to these considerations `naptime::naptime()` either sleeps for the specified duration, or passes forward with a warning. It has the same argument name as `base::Sys.sleep()` and a nearly identical behavior in response to numeric inputs. As such, it can be nearly used as a drop-in replacement for `base::Sys.sleep()`.

# Polymorphic inputs
naptime() accepts a wide variety of inputs. If you find a reasonable input-type for which `naptime::naptime()` doesn't have a reasonable response, please file [an issue](https://github.com/drknexus/naptime/issues).
The one notable exception to identical behavior is the input value of `Inf`. `base::Sys.sleep()` will sleep indefiniately in reponse to `Inf`. In contrast, `naptime::naptime()` will sleep the default duration.

* `numeric` Time in seconds
How do I use it?
================

Options
-------

```r
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.

Polymorphic inputs
==================

naptime() accepts a wide variety of inputs.

Polymorphism for: \* numeric: time in seconds to nap

``` r
naptime(1)
```

* `Period` Any positive time difference regardless of units
* `character` A time in the format of yyyy-mm-dd hh:mm:ss UTC.
- NULL

``` r
naptime(NULL)
#> Warning in nap_warn("NULL passed to naptime(), sleeping for .Options
#> $naptime.default_delay seconds."): NULL passed to naptime(), sleeping
#> for .Options$naptime.default_delay seconds.
#> [1] 0.1
```

- POSIXct: time at which the nap should stop (timezone is respected)

``` r
naptime(lubridate::now()+lubridate::seconds(1))
```

- Period: time from now at which the nap should stop

``` r
naptime(lubridate::seconds(1))
```

- character: ymd\_hms at which nap should stop, time zone is assumed to be the local timezone if discoverable, otherwise UTC

``` r
naptime(as.character(lubridate::now() + lubridate::seconds(1)))
```

- difftime: difference in time to nap

``` r
naptime(difftime(lubridate::now() + seconds(1), lubridate::now()))
```

- logical: nap for default duration

``` r
naptime(TRUE)
#> Warning in nap_warn("Logical passed to naptime(), sleeping for 0
#> seconds."): Logical passed to naptime(), sleeping for 0 seconds.
#> [1] 0.1
```

- generic: nap for default duration

``` r
naptime(glm(rnorm(5) ~ rnorm(5)))
#> Warning in model.matrix.default(mt, mf, contrasts): the response appeared
#> on the right-hand side and was dropped
#> Warning in model.matrix.default(mt, mf, contrasts): problem with term 1 in
#> model.matrix: no columns are assigned
#> Warning in nap_warn("unhandled input for naptime: ", as.character(e), "; sleeping for default duration"): unhandled input for naptime: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'naptime' for signature '"glm"'
#> ; sleeping for default duration
#> [1] 0.1
```

If you find a reasonable input-type for which `naptime::naptime()` doesn't have a reasonable response, please file [an issue](https://github.com/drknexus/naptime/issues).

In addition, naptime will handle some specific errors:
* All negative intervals will be converted to a delay of 0
* All non-finite intervals will be converted to a default delay
* All inputs that produce an error will be converted to a default delay

- All negative intervals will be converted to a delay of 0
- All non-finite intervals will be converted to a default delay
- All inputs that produce an error will be converted to a default delay

How do I get it?
================

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

0 comments on commit 7a9dfd1

Please sign in to comment.