diff --git a/NAMESPACE b/NAMESPACE index 1da8dc4..7e1a34a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -59,6 +59,7 @@ export(test_quietly_that) export(trySplit) export(val) export(val1) +export(validateObject) export(warningp) export(zipit) exportPattern("^[[:alpha:]]+") diff --git a/R/packageDevelopment.R b/R/packageDevelopment.R index fdea580..6c39357 100644 --- a/R/packageDevelopment.R +++ b/R/packageDevelopment.R @@ -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 diff --git a/R/packageLoading.R b/R/packageLoading.R index 952e155..4ae6874 100644 --- a/R/packageLoading.R +++ b/R/packageLoading.R @@ -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() diff --git a/R/validation.R b/R/validation.R new file mode 100644 index 0000000..783bde9 --- /dev/null +++ b/R/validation.R @@ -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(...))) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index c9f84a9..c6e92de 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -37,4 +37,5 @@ reference: - contents: has_keyword("testing") - title: Wrangling - contents: has_keyword("wrangling") - +- title: Validation +- contents: has_keyword("validation") diff --git a/man/stopp.Rd b/man/stopp.Rd index 5af93ca..4fcca04 100644 --- a/man/stopp.Rd +++ b/man/stopp.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/packageDevelopment.R +% Please edit documentation in R/validation.R \name{stopp} \alias{stopp} \title{Custom Stop Function Without Call} @@ -28,4 +28,4 @@ The \code{domain} argument can be used to specify a translation domain. \seealso{ \code{\link[=stop]{stop()}} } -\keyword{packageDevelopment} +\keyword{validation} diff --git a/man/validateObject.Rd b/man/validateObject.Rd new file mode 100644 index 0000000..0de42ba --- /dev/null +++ b/man/validateObject.Rd @@ -0,0 +1,37 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/validation.R +\name{validateObject} +\alias{validateObject} +\title{Validate Object} +\usage{ +validateObject(obj, checks, errorHandler = warning, defaultReturn = NULL) +} +\arguments{ +\item{obj}{The object to validate.} + +\item{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.} + +\item{errorHandler}{A function to handle errors, taking the error message as an argument. Default is \code{warning}.} + +\item{defaultReturn}{The value to return if any check fails. Default is NULL.} +} +\value{ +The original object if all checks pass, or \code{defaultReturn} if any check fails. +} +\description{ +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. +} +\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) +} +\keyword{validation} diff --git a/man/warningp.Rd b/man/warningp.Rd index bda44de..2cd80e3 100644 --- a/man/warningp.Rd +++ b/man/warningp.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/packageDevelopment.R +% Please edit documentation in R/validation.R \name{warningp} \alias{warningp} \title{Custom Warning Function Without Call} @@ -26,4 +26,4 @@ streamlining the output for users. \seealso{ \code{\link[base]{warning}} } -\keyword{packageDevelopment} +\keyword{validation}