-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See #21
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
|
||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |