Skip to content

Commit

Permalink
closes #29
Browse files Browse the repository at this point in the history
- better documentation for `isIncluded()`
- more tests
- more examples
  • Loading branch information
IndrajeetPatil committed Feb 2, 2022
1 parent 34ad62b commit e866a34
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
33 changes: 22 additions & 11 deletions R/error-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,41 @@ isOfType <- function(object, type, nullAllowed = FALSE) {
all(sapply(object, inheritType))
}

#' Check if input is included in a list
#' Check if a vector of values is included in another vector of values
#'
#' @param values Vector of values
#' @param parentValues Vector of values
#' @param values A single value or a vector of values.
#' @param parentValues A single value or a vector of values.
#'
#' @return
#'
#' Returns `TRUE` if the value or **all** `values` (if it's a vector) are
#' present in the `parentValues`; `FALSE` otherwise.
#'
#' @return `TRUE` if the values are inside the parent values.
#' @examples
#' # check if a column is present in dataframe
#' A <- data.frame(
#' col1 = c(1, 2, 3),
#' col2 = c(4, 5, 6),
#' col3 = c(7, 8, 9)
#' )
#' isIncluded("col3", names(A))
#' isIncluded("col3", names(A)) # TRUE
#'
#' # check if single element is present in a vector (atomic or non-atomic)
#' isIncluded("x", list("w", "x", 1, 2)) # TRUE
#' isIncluded("x", c("w", "a", "y")) # FALSE
#'
#' # check if **all** values (if it's a vector) are contained in parent values
#' isIncluded(c("x", "y"), c("a", "y", "b", "x")) # TRUE
#' isIncluded(list("x", 1), list("a", "b", "x", 1)) # TRUE
#' isIncluded(c("x", "y"), c("a", "b", "x")) # FALSE
#' isIncluded(list("x", 1), list("a", "b", "x")) # FALSE
#' @export
isIncluded <- function(values, parentValues) {
if (is.null(values)) {
return(FALSE)
}

if (length(values) == 0) {
if (is.null(values) || length(values) == 0) {
return(FALSE)
}

return(as.logical(min(values %in% parentValues)))
as.logical(min(values %in% parentValues))
}

#' Check if two objects are of same length
Expand Down
24 changes: 18 additions & 6 deletions man/isIncluded.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/validateIsIncluded.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions tests/testthat/test-error-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@ test_that("Checks if type 'is' and 'has' work properly", {
expect_type(isOfType(A, "data.frame"), "logical")
expect_type(isIncluded("col3", names(A)), "logical")

# Output is TRUE
# Output is `TRUE`
expect_true(isSameLength(A, A))
expect_true(isOfLength(A, 3))
expect_true(isIncluded("col3", names(A)))
expect_true(isIncluded(2, 2))
expect_true(isIncluded("x", list("w", "x", 1, 2)))
expect_true(isIncluded(c("x", "y"), c("a", "y", "b", "x")))
expect_true(isIncluded(list("x", "y"), list("a", "b", "x", "y")))
expect_true(isOfType(A, "data.frame"))
expect_true(isOfType(c(1, "x"), c("numeric", "character")))
expect_true(isOfType(NULL, nullAllowed = TRUE))

# Output is FALSE
# Output is `FALSE`
expect_false(isSameLength(A, B))
expect_false(isOfLength(A, 5))
expect_false(isOfType(A, "character"))
expect_false(isIncluded("col4", names(A)))
expect_false(isIncluded(1, 2))
expect_false(isIncluded("x", c("w", "a", "y")))
expect_false(isIncluded(c("x", "y"), c("a", "b", "x")))
expect_false(isIncluded(list("x", "y"), list("a", "b", "x")))
expect_false(isIncluded(NULL))
expect_false(isIncluded(character()))

Expand Down

0 comments on commit e866a34

Please sign in to comment.