Skip to content

Commit

Permalink
Import functions from other packages rather than namespacing. (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek authored Aug 25, 2023
1 parent bed3b93 commit 0449de3
Show file tree
Hide file tree
Showing 37 changed files with 192 additions and 139 deletions.
16 changes: 16 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ S3method(to_lgl,double)
S3method(to_lgl,factor)
S3method(to_lgl,integer)
S3method(to_lgl,logical)
export(caller_arg)
export(caller_env)
export(object_type)
export(stabilize_arg)
export(stabilize_arg_scalar)
Expand All @@ -35,3 +37,17 @@ export(to_int)
export(to_int_scalar)
export(to_lgl)
export(to_lgl_scalar)
importFrom(cli,cli_abort)
importFrom(cli,no)
importFrom(glue,glue)
importFrom(rlang,caller_arg)
importFrom(rlang,caller_env)
importFrom(rlang,check_dots_empty0)
importFrom(rlang,inject)
importFrom(rlang,is_scalar_character)
importFrom(rlang,is_scalar_integer)
importFrom(rlang,is_scalar_logical)
importFrom(rlang,is_scalar_vector)
importFrom(rlang,try_fetch)
importFrom(vctrs,vec_cast)
importFrom(vctrs,vec_size)
28 changes: 14 additions & 14 deletions R/check.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.check_na <- function(x,
allow_na = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
x_arg = caller_arg(x),
call = caller_env()) {
allow_na <- to_lgl_scalar(allow_na, allow_null = FALSE, call = call)
if (allow_na) {
return(invisible(NULL))
Expand All @@ -23,8 +23,8 @@
.check_size <- function(x,
min_size,
max_size,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
x_arg = caller_arg(x),
call = caller_env()) {
if (is.null(min_size) && is.null(max_size)) {
return(invisible(NULL))
}
Expand All @@ -33,7 +33,7 @@
max_size <- to_int_scalar(max_size, call = call)
.check_x_no_more_than_y(min_size, max_size, call = call)

x_size <- vctrs::vec_size(x)
x_size <- vec_size(x)

min_ok <- is.null(min_size) || x_size >= min_size
max_ok <- is.null(max_size) || x_size <= max_size
Expand Down Expand Up @@ -62,8 +62,8 @@
.check_scalar <- function(x,
allow_null = TRUE,
allow_zero_length = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
# TODO: Some of this is redundant.
if (!length(x)) {
Expand All @@ -83,11 +83,11 @@
}
}

if (rlang::is_scalar_vector(x)) {
if (is_scalar_vector(x)) {
return(invisible(NULL))
}

x_size <- vctrs::vec_size(x)
x_size <- vec_size(x)

if (x_class == "NULL") {
x_class <- "non-NULL"
Expand All @@ -96,17 +96,17 @@
"must be a single {.cls {x_class}}.",
x_arg = x_arg,
call = call,
additional_msg = c(x = "{.arg {x_arg}} has {cli::no(x_size)} values.")
additional_msg = c(x = "{.arg {x_arg}} has {no(x_size)} values.")
)
}

.check_x_no_more_than_y <- function(x,
y,
x_arg = rlang::caller_arg(x),
y_arg = rlang::caller_arg(y),
call = rlang::caller_env()) {
x_arg = caller_arg(x),
y_arg = caller_arg(y),
call = caller_env()) {
if (!is.null(x) && !is.null(y) && x > y) {
cli::cli_abort(
cli_abort(
c(
"{.arg {x_arg}} can't be larger than {.arg {y_arg}}.",
"*" = "{.arg {x_arg}} = {x}",
Expand Down
24 changes: 12 additions & 12 deletions R/cls_unexported.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
to_cls_args = list(),
allow_null = TRUE,
allow_zero_length = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
if (is_rlang_cls_scalar(x)) {
return(x)
}

force(x_arg)
force(call)
x <- rlang::inject(
x <- inject(
to_cls_fn(
x,
allow_null = allow_null,
Expand Down Expand Up @@ -44,12 +44,12 @@
allow_na = TRUE,
min_size = NULL,
max_size = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
force(x_arg)
force(call)
x <- rlang::inject(
x <- inject(
to_cls_fn(
x,
allow_null = allow_null,
Expand All @@ -60,7 +60,7 @@
)
)
if (!is.null(check_cls_value_fn)) {
rlang::inject(
inject(
check_cls_value_fn(
x,
!!!check_cls_value_fn_args,
Expand Down Expand Up @@ -90,14 +90,14 @@
allow_null = TRUE,
allow_zero_length = TRUE,
allow_na = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
rlang::check_dots_empty0(..., call = call)
check_dots_empty0(..., call = call)
force(x_arg)
force(call)

x <- rlang::inject(
x <- inject(
to_cls_scalar_fn(
x,
allow_null = allow_null,
Expand All @@ -109,7 +109,7 @@
)
)
if (!is.null(check_cls_value_fn)) {
rlang::inject(
inject(
check_cls_value_fn(
x,
!!!check_cls_value_fn_args,
Expand Down
2 changes: 1 addition & 1 deletion R/is.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.is_allowed_null <- function(x,
allow_null = TRUE,
call = rlang::caller_env()) {
call = caller_env()) {
allow_null <- to_lgl_scalar(allow_null, allow_null = FALSE, call = call)
return(is.null(x) && allow_null)
}
16 changes: 12 additions & 4 deletions R/msgs_common.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#' @importFrom rlang caller_arg
#' @export
rlang::caller_arg

#' @importFrom rlang caller_env
#' @export
rlang::caller_env

.stop_must <- function(msg, x_arg, call, additional_msg = NULL) {
main_msg <- .glue2("{.arg [x_arg]} [msg]")
cli::cli_abort(
cli_abort(
c(main_msg, additional_msg),
call = call,
.envir = rlang::caller_env()
.envir = caller_env()
)
}

Expand All @@ -15,10 +23,10 @@
main_msg <- .glue2(
"Can't coerce {.arg [x_arg]} {.cls [from_class]} to {.cls [to_class]}."
)
cli::cli_abort(
cli_abort(
c(main_msg, additional_msg),
call = call,
.envir = rlang::caller_env()
.envir = caller_env()
)
}

Expand Down
6 changes: 3 additions & 3 deletions R/stabilize_arg.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ stabilize_arg <- function(x,
allow_na = TRUE,
min_size = NULL,
max_size = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
rlang::check_dots_empty0(..., call = call)
check_dots_empty0(..., call = call)

if (is.null(x)) {
return(
Expand Down
6 changes: 3 additions & 3 deletions R/stabilize_arg_scalar.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ stabilize_arg_scalar <- function(x,
allow_null = TRUE,
allow_zero_length = TRUE,
allow_na = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
rlang::check_dots_empty0(..., call = call)
check_dots_empty0(..., call = call)
.check_scalar(
x,
allow_null = allow_null,
Expand Down
8 changes: 4 additions & 4 deletions R/stabilize_chr.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ stabilize_chr <- function(x,
min_size = NULL,
max_size = NULL,
regex = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls(
x,
Expand All @@ -50,8 +50,8 @@ stabilize_chr <- function(x,

.check_value_chr <- function(x,
regex,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
x_arg = caller_arg(x),
call = caller_env()) {
if (is.null(regex)) {
return(invisible(NULL))
}
Expand Down
4 changes: 2 additions & 2 deletions R/stabilize_chr_scalar.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ stabilize_chr_scalar <- function(x,
allow_zero_length = TRUE,
allow_na = TRUE,
regex = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls_scalar(
x,
Expand Down
10 changes: 5 additions & 5 deletions R/stabilize_int.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ stabilize_int <- function(x,
max_size = NULL,
min_value = NULL,
max_value = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls(
x,
Expand All @@ -66,8 +66,8 @@ stabilize_int <- function(x,
.check_value_int <- function(x,
min_value,
max_value,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
x_arg = caller_arg(x),
call = caller_env()) {
min_value <- to_int_scalar(min_value, call = call)
max_value <- to_int_scalar(max_value, call = call)

Expand All @@ -87,5 +87,5 @@ stabilize_int <- function(x,
x = "Values are too high at locations {max_failure_locations}."
)

cli::cli_abort(c(min_msg, max_msg), call = call)
cli_abort(c(min_msg, max_msg), call = call)
}
4 changes: 2 additions & 2 deletions R/stabilize_int_scalar.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ stabilize_int_scalar <- function(x,
coerce_factor = TRUE,
min_value = NULL,
max_value = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls_scalar(
x,
Expand Down
4 changes: 2 additions & 2 deletions R/stabilize_lgl.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ stabilize_lgl <- function(x,
allow_na = TRUE,
min_size = NULL,
max_size = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls(
x,
Expand Down
4 changes: 2 additions & 2 deletions R/stabilize_lgl_scalar.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ stabilize_lgl_scalar <- function(x,
allow_null = TRUE,
allow_zero_length = TRUE,
allow_na = TRUE,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env(),
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)) {
.stabilize_cls_scalar(
x,
Expand Down
12 changes: 12 additions & 0 deletions R/stbl-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom cli cli_abort
#' @importFrom cli no
#' @importFrom glue glue
#' @importFrom rlang check_dots_empty0
#' @importFrom rlang inject
#' @importFrom rlang is_scalar_character
#' @importFrom rlang is_scalar_integer
#' @importFrom rlang is_scalar_logical
#' @importFrom rlang is_scalar_vector
#' @importFrom rlang try_fetch
#' @importFrom vctrs vec_cast
#' @importFrom vctrs vec_size
## usethis namespace: end
NULL
Loading

0 comments on commit 0449de3

Please sign in to comment.