Skip to content

Commit

Permalink
tidyverse style
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Nov 26, 2019
1 parent 2f08dd7 commit 6bbc7cc
Show file tree
Hide file tree
Showing 57 changed files with 154 additions and 145 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: chk
Title: Check User-Supplied Function Arguments
Version: 0.2.0.9003
Version: 0.2.1
Authors@R: c(
person("Joe", "Thorley", , "[email protected]", c("aut", "cre"), comment = c(ORCID = "0000-0002-7683-4592")),
person("Kirill", "M\u00fcller", role = "aut", comment = c(ORCID = "0000-0002-1416-3412")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# chk 0.2.1

## Minor Changes

- Remove benchmarking vignette as causing problems on CRAN.

# chk 0.2.0

## Major Changes
Expand Down
12 changes: 6 additions & 6 deletions R/cc.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#' cc(1:11)
#' cc(as.character(1:2))
cc <- function(x, conj = ", ", sep = ", ",
brac = if(is.character(x) || is.factor(x)) "'" else "",
brac = if (is.character(x) || is.factor(x)) "'" else "",
ellipsis = 10L, chk = TRUE) {
if(chk) {
if (chk) {
chk_string(conj)
chk_string(sep)
chk_s3_class(brac, "character")
Expand All @@ -35,18 +35,18 @@ cc <- function(x, conj = ", ", sep = ", ",
chk_gte(ellipsis, 3)
}

if(!length(x)) {
if (!length(x)) {
return(character(0))
}
x <- p0(brac[1], x, brac[length(brac)])
n <- length(x)
if(n == 1L) {
if (n == 1L) {
return(x)
}
if(n == 2L) {
if (n == 2L) {
return(p(x, collapse = conj))
}
if(n > ellipsis) {
if (n > ellipsis) {
x <- c(x[1:(ellipsis - 2)], "...", x[n])
n <- ellipsis
}
Expand Down
4 changes: 2 additions & 2 deletions R/chk-all-equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#' chk_all_equal(list(c(x = 1), c(x = 1)))
#' try(chk_all_equal(list(c(x = 1), c(y = 1))))
chk_all_equal <- function(x, tolerance = sqrt(.Machine$double.eps), x_name = NULL) {
if(vld_all_equal(x, tolerance = tolerance)) {
if (vld_all_equal(x, tolerance = tolerance)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must have equal elements")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-all-equivalent.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#' chk_all_equivalent(list(c(x = 1), c(x = 1)))
#' chk_all_equivalent(list(c(x = 1), c(y = 1)))
chk_all_equivalent <- function(x, tolerance = sqrt(.Machine$double.eps), x_name = NULL) {
if(vld_all_equivalent(x, tolerance = tolerance)) {
if (vld_all_equivalent(x, tolerance = tolerance)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must have equivalent elements")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-all-identical.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#' chk_all_identical(c(1, 1))
#' try(chk_all_identical(c(1, 1.1)))
chk_all_identical <- function(x, x_name = NULL) {
if(vld_all_identical(x)) {
if (vld_all_identical(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must have identical elements")
}

Expand Down
8 changes: 4 additions & 4 deletions R/chk-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#' # FIXME try(chk_all(1, chk_lgl))
#' chk_all(c(TRUE, NA), chk_lgl)
chk_all <- function(x, chk_fun, ..., x_name = NULL) {
if(is.null(x)) {
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x)) {
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
return(chk_fun(x, ..., x_name = x_name))
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
x_name <- paste0("all elements of ", x_name)

args <- list(...)
Expand All @@ -48,7 +48,7 @@ chk_all <- function(x, chk_fun, ..., x_name = NULL) {
#' # vld_all
#' vld_all(c(TRUE, NA), vld_lgl)
vld_all <- function(x, vld_fun, ...) {
if(is.null(x)) {
if (is.null(x)) {
return(vld_fun(x, ...))
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-atomic.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_atomic(1)
#' try(chk_atomic(list(1)))
chk_atomic <- function(x, x_name = NULL) {
if(vld_atomic(x)) {
if (vld_atomic(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
if (is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
abort_chk(x_name, " must be atomic")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-date.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_date(Sys.Date())
#' try(chk_date(1))
chk_date <- function(x, x_name = NULL) {
if(vld_date(x)) {
if (vld_date(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must be a date (non-missing Date scalar)")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-datetime.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_datetime(as.POSIXct("2001-01-02"))
#' try(chk_datetime(1))
chk_datetime <- function(x, x_name = NULL) {
if(vld_datetime(x)) {
if (vld_datetime(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must be a datetime (non-missing POSIXct scalar)")
}

Expand Down
6 changes: 3 additions & 3 deletions R/chk-dir.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#' chk_dir(tempdir())
#' try(chk_dir(tempfile()))
chk_dir <- function(x, x_name = NULL) {
if(vld_dir(x)) {
if (vld_dir(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
chk_string(x, x_name = x_name)
if(file.exists(x)) {
if (file.exists(x)) {
abort_chk(x_name, " must specify a directory ('", x, "' is a file)")
}
abort_chk(x_name, " must specify an existing directory ('", x, "' can't be found)")
Expand Down
4 changes: 2 additions & 2 deletions R/chk-environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_environment(.GlobalEnv)
#' try(chk_environment(1))
chk_environment <- function(x, x_name = NULL) {
if(vld_environment(x)) {
if (vld_environment(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
if (is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
abort_chk(x_name, " must be an environment")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-equal.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#' try(chk_equal(c(x = 1), c(y = 1L)))
chk_equal <- function(x, y, tolerance = sqrt(.Machine$double.eps),
x_name = NULL) {
if(vld_equal(x, y, tolerance = tolerance)) {
if (vld_equal(x, y, tolerance = tolerance)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
y <- utils::capture.output(dput(y, control = "all"))
abort_chk(x_name, " must be equal to: ", y)
}
Expand Down
4 changes: 2 additions & 2 deletions R/chk-equivalent.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#' chk_equivalent(c(x = 1), c(y = 1))
chk_equivalent <- function(x, y, tolerance = sqrt(.Machine$double.eps),
x_name = NULL) {
if(vld_equivalent(x, y, tolerance = tolerance)) {
if (vld_equivalent(x, y, tolerance = tolerance)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
y <- utils::capture.output(dput(y, control = "all"))
abort_chk(x_name, " must be equivalent to: ", y)
}
Expand Down
10 changes: 6 additions & 4 deletions R/chk-ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#' # chk_ext
#' try(chk_ext("file1.pdf", "png"))
chk_ext <- function(x, ext, x_name = NULL) {
if(vld_ext(x, ext)) {
if (vld_ext(x, ext)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
chk_string(x, x_name = x_name)
abort_chk(x_name, " must have extension ", cc(ext, " or "), " (not '",
tools::file_ext(x), "')")
abort_chk(
x_name, " must have extension ", cc(ext, " or "), " (not '",
tools::file_ext(x), "')"
)
}

#' @describeIn chk_ext Validate File Extension
Expand Down
4 changes: 2 additions & 2 deletions R/chk-false.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_false(FALSE)
#' try(chk_false(0))
chk_false <- function(x, x_name = NULL) {
if(vld_false(x)) {
if (vld_false(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must be FALSE")
}

Expand Down
6 changes: 3 additions & 3 deletions R/chk-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
#' # chk_file
#' try(chk_file(tempfile()))
chk_file <- function(x, x_name = NULL) {
if(vld_file(x)) {
if (vld_file(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
chk_string(x, x_name = x_name)
if(dir.exists(x)) {
if (dir.exists(x)) {
abort_chk(x_name, " must specify a file ('", x, "' is a directory)")
}
abort_chk(x_name, " must specify an existing file ('", x, "' can't be found)")
Expand Down
4 changes: 2 additions & 2 deletions R/chk-flag.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#' chk_flag(TRUE)
#' try(vld_flag(1))
chk_flag <- function(x, x_name = NULL) {
if(vld_flag(x)) {
if (vld_flag(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must be a flag (TRUE or FALSE)")
}

Expand Down
6 changes: 3 additions & 3 deletions R/chk-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#' chk_function(mean)
#' try(chk_function(1))
chk_function <- function(x, formals = NULL, x_name = NULL) {
if(vld_function(x, formals)) {
if (vld_function(x, formals)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if(!is.function(x)) abort_chk(x_name, " must be a function")
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (!is.function(x)) abort_chk(x_name, " must be a function")
abort_chk(x_name, " must have ", formals, " formal arguments (not ", length(formals(x)), ")")
}

Expand Down
6 changes: 3 additions & 3 deletions R/chk-gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#' chk_gt(0.1)
#' try(chk_gt(c(0.1, -0.2)))
chk_gt <- function(x, value = 0, x_name = NULL) {
if(vld_gt(x, value)) {
if (vld_gt(x, value)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if(length(x) == 1L) {
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (length(x) == 1L) {
abort_chk(x_name, " must be greater than ", cc(value), ", not ", cc(x))
}
abort_chk(x_name, " must have values greater than ", cc(value))
Expand Down
6 changes: 3 additions & 3 deletions R/chk-gte.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#' chk_gte(0)
#' try(chk_gte(-0.1))
chk_gte <- function(x, value = 0, x_name = NULL) {
if(vld_gte(x, value)) {
if (vld_gte(x, value)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if(length(x) == 1L) {
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (length(x) == 1L) {
abort_chk(
x_name, " must be greater than or equal to ", cc(value),
", not ", cc(x)
Expand Down
4 changes: 2 additions & 2 deletions R/chk-identical.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#' chk_identical(c(1, 1), c(1, 1))
#' try(chk_identical(1, c(1, 1)))
chk_identical <- function(x, y, x_name = NULL) {
if(identical(x, y)) {
if (identical(x, y)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
y <- utils::capture.output(dput(y, control = "all"))
abort_chk(x_name, " must be identical to: ", y)
}
Expand Down
4 changes: 2 additions & 2 deletions R/chk-lgl.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_lgl(NA)
#' try(chk_lgl(1))
chk_lgl <- function(x, x_name = NULL) {
if(vld_lgl(x)) {
if (vld_lgl(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
if (is.null(x_name)) x_name <- deparse_backtick_chk((substitute(x)))
abort_chk(x_name, " must be a logical scalar (TRUE, FALSE or NA)")
}

Expand Down
4 changes: 2 additions & 2 deletions R/chk-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#' chk_list(list())
#' try(chk_list(1))
chk_list <- function(x, x_name = NULL) {
if(vld_list(x)) {
if (vld_list(x)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(x_name, " must be a list")
}

Expand Down
6 changes: 3 additions & 3 deletions R/chk-lt.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#' chk_lt(-0.1)
#' try(chk_lt(c(-0.1, 0.2)))
chk_lt <- function(x, value = 0, x_name = NULL) {
if(vld_lt(x, value)) {
if (vld_lt(x, value)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if(length(x) == 1L) {
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (length(x) == 1L) {
abort_chk(x_name, " must be less than ", cc(value), ", not ", cc(x))
}
abort_chk(x_name, " must have values less than ", cc(value))
Expand Down
6 changes: 3 additions & 3 deletions R/chk-lte.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#' chk_lte(0)
#' try(chk_lte(0.1))
chk_lte <- function(x, value = 0, x_name = NULL) {
if(vld_lte(x, value)) {
if (vld_lte(x, value)) {
return(invisible())
}
if(is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if(length(x) == 1L) {
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
if (length(x) == 1L) {
abort_chk(
x_name, " must be less than or equal to ", cc(value), ", not ", cc(x),
""
Expand Down
Loading

0 comments on commit 6bbc7cc

Please sign in to comment.