From 7f11fbd417db8ff4c7f3f731de66bd131fab1969 Mon Sep 17 00:00:00 2001 From: vertesy Date: Sun, 7 Jan 2024 01:12:02 +0100 Subject: [PATCH] ... --- NAMESPACE | 2 ++ R/Stringendo.R | 41 +++++++++++++++++++++++++++++++++++++ man/grapes-not-in-grapes.Rd | 25 ++++++++++++++++++++++ man/stopif.Rd | 22 ++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 man/grapes-not-in-grapes.Rd create mode 100644 man/stopif.Rd diff --git a/NAMESPACE b/NAMESPACE index f300a5d..28f573d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export("%!in%") export(AddTrailingDotIfNonePresent) export(AddTrailingSlashfNonePresent) export(FixPath) @@ -46,6 +47,7 @@ export(pps) export(ppu) export(sppp) export(spps) +export(stopif) export(substrRight) export(toCamelCase) export(toDotSeparated) diff --git a/R/Stringendo.R b/R/Stringendo.R index f15f3b4..1d2c6bd 100644 --- a/R/Stringendo.R +++ b/R/Stringendo.R @@ -11,6 +11,47 @@ # try(source("https://raw.githubusercontent.com/vertesy/Stringendo/main/Stringendo.R"), silent = T) + +# ______________________________________________________________________________________________---- +# Control functions ---- +# _________________________________________________________________________________________________ + +# ______________________________________________________________________________________________________________________________ +#' @title Stop Execution If Condition is True +#' +#' @description This function stops the execution of the script if the provided condition evaluates to TRUE. +#' It is the complement of the `stopifnot()` function and is used for asserting conditions where +#' an error should be thrown if the condition is TRUE, rather than FALSE. +#' @param condition A logical condition to be tested. If TRUE, an error message is thrown and execution is stopped. +#' @param message An optional error message to display if the condition is TRUE. +#' +#' @examples a <- 1 +#' stopif(a != 1, message = "A is 1") +#' @export +stopif <- function(condition, message = 'Condition is TRUE.') { + if (isTRUE(condition)) stop(message) +} + + +# ______________________________________________________________________________________________________________________________ +#' @title Negation of '%in%' Operator +#' +#' @description +#' `%!in%` is used to test if elements of one vector are not present in another vector. +#' It is the negation of the `%in%` operator. This operator returns `TRUE` for elements +#' of `x` that are not in `y`. +#' +#' @param x A vector of values to be matched. +#' @param y A vector of values to be matched against. +#' @return A logical vector indicating if elements in `x` are not present in `y`. +#' @examples +#' c(1, 2, 3) %!in% c(2, 4, 6) +#' # [1] TRUE FALSE TRUE +#' @export +'%!in%' <- function(x, y) !('%in%'(x, y)) + + + # ______________________________________________________________________________________________---- # Generic auxiliary functions ---- # _________________________________________________________________________________________________ diff --git a/man/grapes-not-in-grapes.Rd b/man/grapes-not-in-grapes.Rd new file mode 100644 index 0000000..156af31 --- /dev/null +++ b/man/grapes-not-in-grapes.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Stringendo.R +\name{\%!in\%} +\alias{\%!in\%} +\title{Negation of '\%in\%' Operator} +\usage{ +x \%!in\% y +} +\arguments{ +\item{x}{A vector of values to be matched.} + +\item{y}{A vector of values to be matched against.} +} +\value{ +A logical vector indicating if elements in \code{x} are not present in \code{y}. +} +\description{ +\verb{\%!in\%} is used to test if elements of one vector are not present in another vector. +It is the negation of the \code{\%in\%} operator. This operator returns \code{TRUE} for elements +of \code{x} that are not in \code{y}. +} +\examples{ +c(1, 2, 3) \%!in\% c(2, 4, 6) +# [1] TRUE FALSE TRUE +} diff --git a/man/stopif.Rd b/man/stopif.Rd new file mode 100644 index 0000000..f5030dd --- /dev/null +++ b/man/stopif.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Stringendo.R +\name{stopif} +\alias{stopif} +\title{Stop Execution If Condition is True} +\usage{ +stopif(condition, message = "Condition is TRUE.") +} +\arguments{ +\item{condition}{A logical condition to be tested. If TRUE, an error message is thrown and execution is stopped.} + +\item{message}{An optional error message to display if the condition is TRUE.} +} +\description{ +This function stops the execution of the script if the provided condition evaluates to TRUE. +It is the complement of the \code{stopifnot()} function and is used for asserting conditions where +an error should be thrown if the condition is TRUE, rather than FALSE. +} +\examples{ +a <- 1 +stopif(a != 1, message = "A is 1") +}