diff --git a/R/standalone-checks.R b/R/standalone-checks.R index 3e163edc3..ad2537d75 100644 --- a/R/standalone-checks.R +++ b/R/standalone-checks.R @@ -1,7 +1,20 @@ -# THIS SCRIPT MUST OPERATE AS A STANDALONE SCRIPT -# DO NOT USE IMPORTED FUNCTIONS AND ONLY USE rlang AND cli NAMESPACING FOR CHECKS +# DO NOT MODIFY THIS FILE. INSTEAD MODIFY THE VERSION IN https://github.com/ddsjoberg/standalone/tree/main/R +# --- +# repo: ddsjoberg/standalone +# file: standalone-checks.R +# last-updated: 2024-01-24 +# license: https://unlicense.org +# imports: rlang, cli +# --- +# +# This file provides a minimal shim to provide a purrr-like API on top of +# base R functions. They are not drop-in replacements but allow a similar style +# of programming. +# +# ## Changelog # nocov start + #' Check Class #' #' @param class (`character`)\cr @@ -17,6 +30,7 @@ #' Default is `rlang::caller_arg(x)` #' @inheritParams cli::cli_abort #' @keywords internal +#' @noRd check_class <- function(x, class, allow_null = FALSE, arg_name = rlang::caller_arg(x), call = parent.frame()) { # include NULL class as acceptable if allow_null is TRUE @@ -34,6 +48,7 @@ check_class <- function(x, class, allow_null = FALSE, #' #' @inheritParams check_class #' @keywords internal +#' @noRd check_class_data_frame <- function(x, allow_null = FALSE, arg_name = rlang::caller_arg(x), call = parent.frame()) { check_class( @@ -46,7 +61,8 @@ check_class_data_frame <- function(x, allow_null = FALSE, #' #' @inheritParams check_class #' @keywords internal -check_not_missing <- function(x, arg_name = caller_arg(x), call = parent.frame()) { +#' @noRd +check_not_missing <- function(x, arg_name = rlang::caller_arg(x), call = parent.frame()) { if (missing(x)) { cli::cli_abort("The {.arg {arg_name}} argument cannot be missing.", call = call) } @@ -61,19 +77,22 @@ check_not_missing <- function(x, arg_name = caller_arg(x), call = parent.frame() #' integer specifying the required length #' @inheritParams check_class #' @keywords internal -#' @name check_length -NULL - -#' @rdname check_length -check_length <- function(x, length, arg_name = caller_arg(x), call = parent.frame()) { +#' @noRd +check_length <- function(x, length, arg_name = rlang::caller_arg(x), call = parent.frame()) { if (length(x) != length) { cli::cli_abort("The {.arg {arg_name}} argument must be length {.val {length}}.", call = call) } invisible() } -#' @rdname check_length -check_scalar <- function(x, arg_name = caller_arg(x), call = parent.frame()) { +#' Check is Scalar +#' +#' @param msg (`string`)\cr +#' string passed to `cli::cli_abort(message=)` +#' @inheritParams check_class +#' @keywords internal +#' @noRd +check_scalar <- function(x, arg_name = rlang::caller_arg(x), call = parent.frame()) { check_length(x = x, length = 1L, arg_name = arg_name, call = call) } @@ -88,6 +107,7 @@ check_scalar <- function(x, arg_name = caller_arg(x), call = parent.frame()) { #' #' @return invisible #' @keywords internal +#' @noRd check_range <- function(x, range, include_bounds = c(FALSE, FALSE), @@ -145,6 +165,7 @@ check_range <- function(x, #' #' @return invisible #' @keywords internal +#' @noRd check_binary <- function(x, arg_name = caller_arg(x), call = parent.frame()) { if (!is.logical(x) && !(is_integerish(x) && is_empty(setdiff(x, c(0, 1, NA))))) { paste( @@ -158,5 +179,4 @@ check_binary <- function(x, arg_name = caller_arg(x), call = parent.frame()) { } - # nocov end diff --git a/R/standalone-forcats.R b/R/standalone-forcats.R index d5fa7c067..b17baaad7 100644 --- a/R/standalone-forcats.R +++ b/R/standalone-forcats.R @@ -1,8 +1,28 @@ -# In the style of rlang's standalone-purrr.R, this file provides a minimal shim -# to provide a forcats-like API on top of base R functions. - +# DO NOT MODIFY THIS FILE. INSTEAD MODIFY THE VERSION IN https://github.com/ddsjoberg/standalone/tree/main/R +# --- +# file: standalone-forcats.R +# last-updated: 2024-01-24 +# license: https://unlicense.org +# imports: +# --- +# +# This file provides a minimal shim to provide a forcats-like API on top of +# base R functions. They are not drop-in replacements but allow a similar style +# of programming. +# +# ## Changelog +# # nocov start +fct_infreq <- function(f, ordered = NA) { + # reorder by frequency + factor( + f, + levels = table(f) |> sort(decreasing = TRUE) |> names(), + ordered = ifelse(is.na(ordered), is.ordered(f), ordered) + ) +} + fct_inorder <- function(f, ordered = NA) { factor( f, diff --git a/R/standalone-purrr.R b/R/standalone-purrr.R index 2f6094af1..9cb36e941 100644 --- a/R/standalone-purrr.R +++ b/R/standalone-purrr.R @@ -165,9 +165,7 @@ every <- function(.x, .p, ...) { .p <- as_function(.p, env = global_env()) for (i in seq_along(.x)) { - if (!is_true(.p(.x[[i]], ...))) { - return(FALSE) - } + if (!rlang::is_true(.p(.x[[i]], ...))) return(FALSE) } TRUE } @@ -175,9 +173,7 @@ some <- function(.x, .p, ...) { .p <- as_function(.p, env = global_env()) for (i in seq_along(.x)) { - if (is_true(.p(.x[[i]], ...))) { - return(TRUE) - } + if (rlang::is_true(.p(.x[[i]], ...))) return(TRUE) } FALSE } diff --git a/R/standalone-stringr.R b/R/standalone-stringr.R index 1baf52de4..afe42bced 100644 --- a/R/standalone-stringr.R +++ b/R/standalone-stringr.R @@ -1,6 +1,17 @@ -# In the style of rlang's standalone-purrr.R, this file provides a minimal shim -# to provide a stringr-like API on top of base R functions. - +# DO NOT MODIFY THIS FILE. INSTEAD MODIFY THE VERSION IN https://github.com/ddsjoberg/standalone/tree/main/R +# --- +# file: standalone-stringr.R +# last-updated: 2024-01-24 +# license: https://unlicense.org +# imports: +# --- +# +# This file provides a minimal shim to provide a stringr-like API on top of +# base R functions. They are not drop-in replacements but allow a similar style +# of programming. +# +# ## Changelog +# # nocov start str_trim <- function(string, side = c("both", "left", "right")) { @@ -28,6 +39,3 @@ str_extract <- function(string, pattern) { str_detect <- function(string, pattern) { grepl(pattern = pattern, x = string) } - - -# nocov end diff --git a/_pkgdown.yml b/_pkgdown.yml index 933559c35..d1c91bbac 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -12,6 +12,14 @@ template: # icon: fa-github # href: https://github.com/insightsengineering/cardx +development: + mode: auto + version_label: default + +authors: + Daniel D. Sjoberg: + href: "http://www.danieldsjoberg.com/" + reference: - title: "ARD Creation" - subtitle: "Inference" diff --git a/man/check_binary.Rd b/man/check_binary.Rd deleted file mode 100644 index c5e6600b8..000000000 --- a/man/check_binary.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_binary} -\alias{check_binary} -\title{Check Binary} -\usage{ -check_binary(x, arg_name = caller_arg(x), call = parent.frame()) -} -\arguments{ -\item{x}{a vector} - -\item{call}{call environment} -} -\value{ -invisible -} -\description{ -Checks if a column in a data frame is binary, -that is, if the column is class \verb{} or -\verb{} and coded as \code{c(0, 1)} -} -\keyword{internal} diff --git a/man/check_class.Rd b/man/check_class.Rd deleted file mode 100644 index 295ca6294..000000000 --- a/man/check_class.Rd +++ /dev/null @@ -1,48 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_class} -\alias{check_class} -\title{Check Class} -\usage{ -check_class( - x, - class, - allow_null = FALSE, - arg_name = rlang::caller_arg(x), - call = parent.frame() -) -} -\arguments{ -\item{x}{\code{(object)}\cr -object to check} - -\item{class}{(\code{character})\cr -character vector or string indicating accepted classes. -Passed to \code{inherits(what=class)}} - -\item{allow_null}{(\code{logical(1)})\cr -Logical indicating whether a NULL value will pass the test. -Default is \code{FALSE}} - -\item{arg_name}{(\code{string})\cr -string indicating the label/symbol of the object being checked. -Default is \code{rlang::caller_arg(x)}} - -\item{call}{The execution environment of a currently running -function, e.g. \code{call = caller_env()}. The corresponding function -call is retrieved and mentioned in error messages as the source -of the error. - -You only need to supply \code{call} when throwing a condition from a -helper function which wouldn't be relevant to mention in the -message. - -Can also be \code{NULL} or a \link[rlang:topic-defuse]{defused function call} to -respectively not display any call or hard-code a code to display. - -For more information about error calls, see \ifelse{html}{\link[rlang:topic-error-call]{Including function calls in error messages}}{\link[rlang:topic-error-call]{Including function calls in error messages}}.} -} -\description{ -Check Class -} -\keyword{internal} diff --git a/man/check_class_data_frame.Rd b/man/check_class_data_frame.Rd deleted file mode 100644 index 01b7128b3..000000000 --- a/man/check_class_data_frame.Rd +++ /dev/null @@ -1,43 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_class_data_frame} -\alias{check_class_data_frame} -\title{Check Class Data Frame} -\usage{ -check_class_data_frame( - x, - allow_null = FALSE, - arg_name = rlang::caller_arg(x), - call = parent.frame() -) -} -\arguments{ -\item{x}{\code{(object)}\cr -object to check} - -\item{allow_null}{(\code{logical(1)})\cr -Logical indicating whether a NULL value will pass the test. -Default is \code{FALSE}} - -\item{arg_name}{(\code{string})\cr -string indicating the label/symbol of the object being checked. -Default is \code{rlang::caller_arg(x)}} - -\item{call}{The execution environment of a currently running -function, e.g. \code{call = caller_env()}. The corresponding function -call is retrieved and mentioned in error messages as the source -of the error. - -You only need to supply \code{call} when throwing a condition from a -helper function which wouldn't be relevant to mention in the -message. - -Can also be \code{NULL} or a \link[rlang:topic-defuse]{defused function call} to -respectively not display any call or hard-code a code to display. - -For more information about error calls, see \ifelse{html}{\link[rlang:topic-error-call]{Including function calls in error messages}}{\link[rlang:topic-error-call]{Including function calls in error messages}}.} -} -\description{ -Check Class Data Frame -} -\keyword{internal} diff --git a/man/check_length.Rd b/man/check_length.Rd deleted file mode 100644 index 61343ad1c..000000000 --- a/man/check_length.Rd +++ /dev/null @@ -1,43 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_length} -\alias{check_length} -\alias{check_scalar} -\title{Check Length} -\usage{ -check_length(x, length, arg_name = caller_arg(x), call = parent.frame()) - -check_scalar(x, arg_name = caller_arg(x), call = parent.frame()) -} -\arguments{ -\item{x}{\code{(object)}\cr -object to check} - -\item{length}{(\code{integer(1)})\cr -integer specifying the required length} - -\item{arg_name}{(\code{string})\cr -string indicating the label/symbol of the object being checked. -Default is \code{rlang::caller_arg(x)}} - -\item{call}{The execution environment of a currently running -function, e.g. \code{call = caller_env()}. The corresponding function -call is retrieved and mentioned in error messages as the source -of the error. - -You only need to supply \code{call} when throwing a condition from a -helper function which wouldn't be relevant to mention in the -message. - -Can also be \code{NULL} or a \link[rlang:topic-defuse]{defused function call} to -respectively not display any call or hard-code a code to display. - -For more information about error calls, see \ifelse{html}{\link[rlang:topic-error-call]{Including function calls in error messages}}{\link[rlang:topic-error-call]{Including function calls in error messages}}.} - -\item{msg}{(\code{string})\cr -string passed to \code{cli::cli_abort(message=)}} -} -\description{ -Check Length -} -\keyword{internal} diff --git a/man/check_not_missing.Rd b/man/check_not_missing.Rd deleted file mode 100644 index 0ad724ffb..000000000 --- a/man/check_not_missing.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_not_missing} -\alias{check_not_missing} -\title{Check Argument not Missing} -\usage{ -check_not_missing(x, arg_name = caller_arg(x), call = parent.frame()) -} -\arguments{ -\item{x}{\code{(object)}\cr -object to check} - -\item{arg_name}{(\code{string})\cr -string indicating the label/symbol of the object being checked. -Default is \code{rlang::caller_arg(x)}} - -\item{call}{The execution environment of a currently running -function, e.g. \code{call = caller_env()}. The corresponding function -call is retrieved and mentioned in error messages as the source -of the error. - -You only need to supply \code{call} when throwing a condition from a -helper function which wouldn't be relevant to mention in the -message. - -Can also be \code{NULL} or a \link[rlang:topic-defuse]{defused function call} to -respectively not display any call or hard-code a code to display. - -For more information about error calls, see \ifelse{html}{\link[rlang:topic-error-call]{Including function calls in error messages}}{\link[rlang:topic-error-call]{Including function calls in error messages}}.} -} -\description{ -Check Argument not Missing -} -\keyword{internal} diff --git a/man/check_range.Rd b/man/check_range.Rd deleted file mode 100644 index edff80054..000000000 --- a/man/check_range.Rd +++ /dev/null @@ -1,37 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/standalone-checks.R -\name{check_range} -\alias{check_range} -\title{Check Range} -\usage{ -check_range( - x, - range, - include_bounds = c(FALSE, FALSE), - arg_name = caller_arg(x), - scalar = FALSE, - msg = paste("The {.arg {arg_name}} argument must be in the interval", - "{.code {ifelse(include_bounds[1], '[', '(')}{range[1]},", - "{range[2]}{ifelse(include_bounds[2], ']', ')')}}."), - call = parent.frame() -) -} -\arguments{ -\item{x}{numeric scalar to check} - -\item{range}{numeric vector of length two} - -\item{include_bounds}{logical of length two indicating whether to allow -the lower and upper bounds} - -\item{scalar}{logical indicating whether \code{x} must be a scalar} - -\item{msg}{string passed to \code{cli::cli_abort(message=)}} -} -\value{ -invisible -} -\description{ -Check Range -} -\keyword{internal}