Skip to content

Commit

Permalink
Replace is.atomic calls with checkmate equivalents (#186)
Browse files Browse the repository at this point in the history
Closes insightsengineering/coredev-tasks#508

`is.atomic` breaks for the `NULL` input in R devel. However, checkmate
retains the past behavior, so we should replace them when `NULL` is
possible as inputs.

---------

Signed-off-by: Vedha Viyash <[email protected]>
Co-authored-by: André Veríssimo <[email protected]>
  • Loading branch information
vedhav and averissimo authored Feb 13, 2024
1 parent c514ca0 commit e20f72d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
16 changes: 11 additions & 5 deletions R/choices_selected.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ choices_selected <- function(choices,
selected = if (inherits(choices, "delayed_data")) NULL else choices[1],
keep_order = FALSE,
fixed = FALSE) {
stopifnot(is.atomic(choices) || inherits(choices, "delayed_data"))
stopifnot(is.atomic(selected) || inherits(selected, "delayed_data") || inherits(selected, "all_choices"))
checkmate::assert(
checkmate::check_atomic(choices),
checkmate::check_class(choices, "delayed_data")
)
checkmate::assert(
checkmate::check_atomic(selected),
checkmate::check_multi_class(selected, c("delayed_data", "all_choices"))
)
checkmate::assert_flag(keep_order)
checkmate::assert_flag(fixed)

Expand Down Expand Up @@ -223,7 +229,7 @@ no_selected_as_NULL <- function(x) { # nolint
## Non-exported utils functions ----
## Modify vectors and keep attributes
vector_reorder <- function(vec, idx) {
stopifnot(is.atomic(vec))
checkmate::assert_atomic(vec)
checkmate::assert_integer(idx, min.len = 1, lower = 1, any.missing = FALSE)
stopifnot(length(vec) == length(idx))

Expand All @@ -243,7 +249,7 @@ vector_reorder <- function(vec, idx) {
}

vector_pop <- function(vec, idx) {
stopifnot(is.atomic(vec))
checkmate::assert_atomic(vec)
checkmate::assert_integer(idx, lower = 1, any.missing = FALSE)

if (length(idx) == 0) {
Expand All @@ -265,7 +271,7 @@ vector_pop <- function(vec, idx) {
}

vector_remove_dups <- function(vec) {
stopifnot(is.atomic(vec))
checkmate::assert_atomic(vec)

idx <- which(duplicated(vec))

Expand Down
2 changes: 1 addition & 1 deletion R/resolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ resolve_delayed_expr <- function(x, ds, is_value_choices) {

# check returned value
if (is_value_choices) {
if (!is.atomic(res) || anyDuplicated(res)) {
if (!checkmate::test_atomic(res) || anyDuplicated(res)) {
stop(paste(
"The following function must return a vector with unique values",
"from the respective columns of the dataset.\n\n",
Expand Down
24 changes: 19 additions & 5 deletions R/select_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ select_spec.delayed_data <- function(choices, # nolint
always_selected = NULL,
ordered = FALSE,
label = NULL) {
stopifnot(is.null(selected) || is.atomic(selected) || inherits(selected, "delayed_data"))
stopifnot(is.null(choices) || is.atomic(choices) || inherits(choices, "delayed_data"))
checkmate::assert(
checkmate::check_null(selected),
checkmate::check_atomic(selected),
checkmate::check_class(selected, "delayed_data")
)
checkmate::assert(
checkmate::check_null(choices),
checkmate::check_atomic(choices),
checkmate::check_class(choices, "delayed_data")
)

structure(
list(
Expand All @@ -145,8 +153,14 @@ select_spec.default <- function(choices, # nolint
always_selected = NULL,
ordered = FALSE,
label = NULL) {
stopifnot(is.null(choices) || is.atomic(choices))
stopifnot(is.null(selected) || is.atomic(selected))
checkmate::assert(
checkmate::check_null(choices),
checkmate::check_atomic(choices)
)
checkmate::assert(
checkmate::check_null(selected),
checkmate::check_atomic(selected)
)

# if names is NULL, shiny will put strange labels (with quotes etc.) in the selectInputs, so we set it to the values
if (is.null(names(choices))) {
Expand All @@ -155,7 +169,7 @@ select_spec.default <- function(choices, # nolint

# Deal with selected
if (length(selected) > 0) {
stopifnot(is.atomic(selected))
checkmate::assert_atomic(selected)
checkmate::assert_subset(selected, choices)
stopifnot(multiple || length(selected) == 1)
if (is.null(names(selected))) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param sep (`character`) Separator
#' @export
split_by_sep <- function(x, sep) {
stopifnot(is.atomic(x))
checkmate::assert_atomic(x)
if (is.character(x)) {
strsplit(x, sep, fixed = TRUE)
} else {
Expand Down

0 comments on commit e20f72d

Please sign in to comment.