Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Jan 27, 2024
1 parent c8a696c commit eea829a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 253 deletions.
42 changes: 31 additions & 11 deletions R/standalone-checks.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand All @@ -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),
Expand Down Expand Up @@ -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(
Expand All @@ -158,5 +179,4 @@ check_binary <- function(x, arg_name = caller_arg(x), call = parent.frame()) {
}



# nocov end
26 changes: 23 additions & 3 deletions R/standalone-forcats.R
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 2 additions & 6 deletions R/standalone-purrr.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,15 @@ 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
}
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
}
Expand Down
20 changes: 14 additions & 6 deletions R/standalone-stringr.R
Original file line number Diff line number Diff line change
@@ -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")) {
Expand Down Expand Up @@ -28,6 +39,3 @@ str_extract <- function(string, pattern) {
str_detect <- function(string, pattern) {
grepl(pattern = pattern, x = string)
}


# nocov end
8 changes: 8 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 0 additions & 22 deletions man/check_binary.Rd

This file was deleted.

48 changes: 0 additions & 48 deletions man/check_class.Rd

This file was deleted.

43 changes: 0 additions & 43 deletions man/check_class_data_frame.Rd

This file was deleted.

43 changes: 0 additions & 43 deletions man/check_length.Rd

This file was deleted.

34 changes: 0 additions & 34 deletions man/check_not_missing.Rd

This file was deleted.

Loading

0 comments on commit eea829a

Please sign in to comment.