Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions R/standalone-obj-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#
# ## Changelog
#
# 2025-10-02:
# - `obj_type_friendly()` now shows the dimensionality of arrays.
#
# 2024-02-14:
# - `obj_type_friendly()` now works for S7 objects.
#
Expand Down Expand Up @@ -183,14 +186,16 @@ vec_type_friendly <- function(x, length = FALSE) {
}

if (type == "list") {
if (n_dim < 2) {
if (n_dim == 0) {
return(add_length("a list"))
} else if (is.data.frame(x)) {
return("a data frame")
} else if (n_dim == 2) {
return("a list matrix")
if (is.data.frame(x)) {
return("a data frame")
} else {
return("a list matrix")
}
} else {
return("a list array")
return(sprintf("a list %sD array", n_dim))
}
}

Expand All @@ -206,12 +211,12 @@ vec_type_friendly <- function(x, length = FALSE) {
type = paste0("a ", type, " %s")
)

if (n_dim < 2) {
if (n_dim == 0) {
kind <- "vector"
} else if (n_dim == 2) {
kind <- "matrix"
} else {
kind <- "array"
kind <- sprintf("%sD array", n_dim)
}
out <- sprintf(type, kind)

Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/_snaps/standalone-obj-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,41 @@
Error in `checker()`:
! Element 1 of `x` must be a logical, not the number 1.

# stop_input_type() reports 1D arrays differently from vectors

Code
err(checker(array(1), stop_input_type, what = "a double vector", arg = "x"))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must be a double vector, not a double 1D array.

---

Code
err(checker(array(list(1)), stop_input_type, what = "a list", arg = "x"))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must be a list, not a list 1D array.

# stop_input_type() can differentiate between arrays by dimensionality

Code
err(checker(array(1, dim = c(1, 1, 1, 1)), stop_input_type, what = "a double 3D array",
arg = "x"))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must be a double 3D array, not a double 4D array.

---

Code
err(checker(array(list(1), dim = c(1, 1, 1, 1)), stop_input_type, what = "a list 3D array",
arg = "x"))
Output
<error/rlang_error>
Error in `checker()`:
! `x` must be a list 3D array, not a list 4D array.

19 changes: 14 additions & 5 deletions tests/testthat/test-friendly-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ test_that("obj_type_friendly() only displays the first class of objects", {
})

test_that("obj_type_friendly() supports matrices and arrays (#141)", {
expect_true(all(friendly_types(matrix(list(1, 2))) == "a list matrix"))
# 1D, 2D, and >=3D all vary!

expect_true(all(friendly_types(array(list(1, 2, 3))) == "a list 1D array"))
expect_true(all(friendly_types(matrix(list(1, 2, 3))) == "a list matrix"))
expect_true(all(
friendly_types(array(list(1, 2, 3), dim = 1:3)) == "a list array"
friendly_types(array(list(1, 2, 3), dim = 1:3)) == "a list 3D array"
))

expect_true(all(friendly_types(array(1:3)) == "an integer 1D array"))
expect_true(all(friendly_types(matrix(1:3)) == "an integer matrix"))
expect_true(all(friendly_types(array(1:3, dim = 1:3)) == "an integer array"))
expect_true(all(
friendly_types(array(1:3, dim = 1:3)) == "an integer 3D array"
))

expect_true(all(friendly_types(matrix(letters)) == "a character matrix"))
expect_true(all(
friendly_types(array(letters[1:3], dim = 1:3)) == "a character array"
friendly_types(array(letters[1:3])) == "a character 1D array"
))
expect_true(all(friendly_types(matrix(letters[1:3])) == "a character matrix"))
expect_true(all(
friendly_types(array(letters[1:3], dim = 1:3)) == "a character 3D array"
))
})

Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-standalone-obj-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,41 @@ test_that("stop_input_type() handles I() in `arg` (#1607)", {
))
})
})

test_that("stop_input_type() reports 1D arrays differently from vectors", {
expect_snapshot({
err(checker(
array(1),
stop_input_type,
what = "a double vector",
arg = "x"
))
})
expect_snapshot({
err(checker(
array(list(1)),
stop_input_type,
what = "a list",
arg = "x"
))
})
})

test_that("stop_input_type() can differentiate between arrays by dimensionality", {
expect_snapshot({
err(checker(
array(1, dim = c(1, 1, 1, 1)),
stop_input_type,
what = "a double 3D array",
arg = "x"
))
})
expect_snapshot({
err(checker(
array(list(1), dim = c(1, 1, 1, 1)),
stop_input_type,
what = "a list 3D array",
arg = "x"
))
})
})