Skip to content

Commit

Permalink
assert_non_na()
Browse files Browse the repository at this point in the history
See #21
  • Loading branch information
wibeasley committed Mar 24, 2017
1 parent 2e2b6a5 commit 0d158ae
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(assert_non_na)
export(clump_month_date)
export(column_class_headstart)
export(column_rename_headstart)
Expand Down
42 changes: 42 additions & 0 deletions R/assert.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' @name assert
#' @title Assert vector characteristics
#'
#' @description Assert a vector meets important data-quality requirements.
#'
#' @param x Vector to inspect. Required.
#' @param class_vector The required [base::class()] of the vector. If the parameter is missing, the object's class is not checked.
#' @param proportion_minimum The (inclusive) minimum proportion of the vector's elements that should meet the requirement. If missing, all elements must pass.
#'
#' @examples
#' requireNamespace("OuhscMunge")
#' OuhscMunge::assert_non_na(1:30, "integer")
#' \dontrun{
#' OuhscMunge::assert_non_na(c(1:30, NA_integer_), "integer")
#' }


#' @export
assert_non_na <- function( x, class_vector, proportion_minimum ) {

if( !missing(class_vector) & !inherits(x, class_vector) ) {
stop("The vector must inherit from the class `", class_vector, "`, but it is a `", class(x), "`.")
}


if( missing(proportion_minimum) ) {

missing_count <- sum(is.na(x))
if( missing_count > 0L ) {
stop("The vector should not have any NA values, but `", missing_count, "` elements were NA.")
}

} else {

proportion_actual <- mean(!is.na(x))
if( proportion_actual < proportion_minimum) {
stop("The vector must have a proportion of at least `", proportion_minimum, "` of nonmissing elements. However the actual nonmissing proportion is `", proportion_actual, "1.")
}

}

}
26 changes: 26 additions & 0 deletions man/assert.Rd

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

50 changes: 50 additions & 0 deletions tests/testthat/test-assert.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(testthat)

###########
context("Assert")
###########

test_that("assert_non_na -all - passes", {
expect_silent( assert_non_na(1:100 , "integer" ))
expect_silent( assert_non_na(runif(100) , "numeric" ))
expect_silent( assert_non_na(letters , "character" ))
expect_silent( assert_non_na(sample(c(T,F), size=100, replace = TRUE) , "logical" ))
expect_silent( assert_non_na(seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") , "Date" ))

expect_silent( assert_non_na(1:100 , "integer" , 1.0))
expect_silent( assert_non_na(runif(100) , "numeric" , 1.0))
expect_silent( assert_non_na(letters , "character" , 1.0))
expect_silent( assert_non_na(sample(c(T,F), size=100, replace = TRUE) , "logical" , 1.0))
expect_silent( assert_non_na(seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") , "Date" , 1.0))
})

test_that("assert_non_na -all - fails", {
expect_error( assert_non_na(c(NA, 1:100 ) , "integer" ))
expect_error( assert_non_na(c(NA, runif(100) ) , "numeric" ))
expect_error( assert_non_na(c(NA, letters ) , "character" ))
expect_error( assert_non_na(c(NA, sample(c(T,F), size=100, replace = TRUE) ) , "logical" ))
expect_error( assert_non_na(c(as.Date(NA), seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") ) , "Date" ))
})


test_that("assert_non_na -proportion - passes", {
expect_silent( assert_non_na(1:1 , "integer" , 1.0))
expect_silent( assert_non_na(runif(100) , "numeric" , 1.0))
expect_silent( assert_non_na(letters , "character" , 1.0))
expect_silent( assert_non_na(sample(c(T,F), size=100, replace = TRUE) , "logical" , 1.0))
expect_silent( assert_non_na(seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") , "Date" , 1.0))

expect_silent( assert_non_na(c(NA, 1:100 ) , "integer" , 0.5))
expect_silent( assert_non_na(c(NA, runif(100) ) , "numeric" , 0.5))
expect_silent( assert_non_na(c(NA, letters ) , "character" , 0.5))
expect_silent( assert_non_na(c(NA, sample(c(T,F), size=100, replace = TRUE) ) , "logical" , 0.5))
expect_silent( assert_non_na(c(as.Date(NA), seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") ) , "Date" , 0.5))
})

test_that("assert_non_na -proportion - fails", {
expect_error( assert_non_na(c(NA, 1:100 ) , "integer" , 0.999))
expect_error( assert_non_na(c(NA, runif(100) ) , "numeric" , 0.999))
expect_error( assert_non_na(c(NA, letters ) , "character" , 0.999))
expect_error( assert_non_na(c(NA, sample(c(T,F), size=100, replace = TRUE) ) , "logical" , 0.999))
expect_error( assert_non_na(c(as.Date(NA), seq.Date(as.Date("2015-01-02"), as.Date("2017-04-30"), by="days") ) , "Date" , 0.999))
})

0 comments on commit 0d158ae

Please sign in to comment.