Skip to content

Commit

Permalink
added validation script
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Jun 22, 2024
1 parent 16b06b2 commit f9f86c1
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 50 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(test_quietly_that)
export(trySplit)
export(val)
export(val1)
export(validateObject)
export(warningp)
export(zipit)
exportPattern("^[[:alpha:]]+")
Expand Down
43 changes: 0 additions & 43 deletions R/packageDevelopment.R
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
#' Custom Stop Function Without Call
#'
#' This function provides a wrapper around the base \code{\link[base]{stop}} function,
#' but it automatically sets \code{call.} to FALSE, which means the function call itself
#' is not included in the resulting error message. This makes error messages cleaner.
#' The \code{domain} argument can be used to specify a translation domain.
#'
#' @param ... Arguments passed on to \code{stop}.
#' @param domain The translation domain, NULL by default.
#'
#' @return No return value, this function stops execution of the program.
#' @export
#' @keywords packageDevelopment
#' @seealso [stop()]
#' @examples
#' \dontrun{
#' stopp("This is a custom stop message without the call.")
#' }
#' @export
stopp <- function(..., domain = NULL) {
stop(..., call. = FALSE, domain = domain)
}

#' Custom Warning Function Without Call
#'
#' This function provides a wrapper around the base \code{\link[base]{warning}} function,
#' adding flexibility to warnings by setting \code{call.} to FALSE automatically. This
#' modification means that the function call is not included in the warning message,
#' streamlining the output for users.
#'
#' @param ... Arguments passed on to \code{warning}.
#' @return No return value, this function issues a warning.
#' @export
#' @keywords packageDevelopment
#' @seealso \code{\link[base]{warning}}
#' @examples
#' \dontrun{
#' warningp("This is a custom warning message without the call.")
#' }
warningp <- function(...) {
do.call(base::warning, args = append(list(call. = FALSE), list(...)))
}

#' Get Keywords from R Package Documentation
#'
#' @description
Expand Down
6 changes: 4 additions & 2 deletions R/packageLoading.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ installAndLoad <- function(
library(package, character.only = TRUE)
}

for (el in zipi(list("CRAN", "Bioconductor", "GitHub"), list(cran, bioc, gh))) {
sapply(el[[2]], function(pkg) capture.output(suppressMessages((install_and_load(pkg, el[[1]])))))
for (el in zipit(list("CRAN", "Bioconductor", "GitHub"), list(cran, bioc, gh))) {
sapply(el[[2]], function(pkg)
utils::capture.output(suppressMessages((install_and_load(pkg, el[[1]]))))
)
}

invisible()
Expand Down
77 changes: 77 additions & 0 deletions R/validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#' Validate Object
#'
#' This function validates an object using a list of checks. If any check fails, an error handler is called and a default value is returned.
#'
#' @param obj The object to validate.
#' @param checks A list of functions, each taking the object as an argument and returning NULL if the check passes or an error message if the check fails.
#' @param errorHandler A function to handle errors, taking the error message as an argument. Default is `warning`.
#' @param defaultReturn The value to return if any check fails. Default is NULL.
#'
#' @return The original object if all checks pass, or `defaultReturn` if any check fails.
#' @export
#' @keywords validation
#' @examples
#' # Define some checks
#' checkNotNull <- function(x) if (is.null(x)) "Object is NULL" else NULL
#' checkIsNumeric <- function(x) if (!is.numeric(x)) "Object is not numeric" else NULL
#'
#' # Validate an object
#' obj <- 42
#' validateObject(obj, list(checkNotNull, checkIsNumeric))
#'
#' # Validate an object that fails a check
#' obj <- NULL
#' try(validateObject(obj, list(checkNotNull, checkIsNumeric), errorHandler = stop), silent = TRUE)
validateObject <- function(obj, checks, errorHandler = warning, defaultReturn = NULL) {
for (check in checks) {
message <- check(obj)
if (!is.null(message)) {
errorHandler(message)
return(defaultReturn)
}
}
return(obj)
}

#' Custom Stop Function Without Call
#'
#' This function provides a wrapper around the base \code{\link[base]{stop}} function,
#' but it automatically sets \code{call.} to FALSE, which means the function call itself
#' is not included in the resulting error message. This makes error messages cleaner.
#' The \code{domain} argument can be used to specify a translation domain.
#'
#' @param ... Arguments passed on to \code{stop}.
#' @param domain The translation domain, NULL by default.
#'
#' @return No return value, this function stops execution of the program.
#' @export
#' @keywords validation
#' @seealso [stop()]
#' @examples
#' \dontrun{
#' stopp("This is a custom stop message without the call.")
#' }
#' @export
stopp <- function(..., domain = NULL) {
stop(..., call. = FALSE, domain = domain)
}

#' Custom Warning Function Without Call
#'
#' This function provides a wrapper around the base \code{\link[base]{warning}} function,
#' adding flexibility to warnings by setting \code{call.} to FALSE automatically. This
#' modification means that the function call is not included in the warning message,
#' streamlining the output for users.
#'
#' @param ... Arguments passed on to \code{warning}.
#' @return No return value, this function issues a warning.
#' @export
#' @keywords validation
#' @seealso \code{\link[base]{warning}}
#' @examples
#' \dontrun{
#' warningp("This is a custom warning message without the call.")
#' }
warningp <- function(...) {
do.call(base::warning, args = append(list(call. = FALSE), list(...)))
}
3 changes: 2 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ reference:
- contents: has_keyword("testing")
- title: Wrangling
- contents: has_keyword("wrangling")

- title: Validation
- contents: has_keyword("validation")
4 changes: 2 additions & 2 deletions man/stopp.Rd

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

37 changes: 37 additions & 0 deletions man/validateObject.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/warningp.Rd

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

0 comments on commit f9f86c1

Please sign in to comment.