Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions R/standalone-obj-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#
# ## Changelog
#
# 2025-09-30:
# - `obj_type_friendly()` now handles 1D arrays differently from vectors.
#
# 2024-02-14:
# - `obj_type_friendly()` now works for S7 objects.
#
Expand Down Expand Up @@ -183,8 +186,10 @@ 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 (n_dim == 1) {
return("a list array")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return("a list array")
return("an array vector of type list")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • n_dim = 0 gives vector
  • n_dim = 2 gives matrix
  • n_dim = N gives {N}D array

Here's what we agreed on

I like this because it also lets you differentiate between arrays of different dimensionality which we couldn't do before. Like "i expect a 3D array, not a 4D array"

} else if (is.data.frame(x)) {
return("a data frame")
} else if (n_dim == 2) {
Expand All @@ -206,8 +211,10 @@ 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 == 1) {
kind <- "array"
} else if (n_dim == 2) {
kind <- "matrix"
} else {
Expand Down
18 changes: 18 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,21 @@
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 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 array.

9 changes: 7 additions & 2 deletions tests/testthat/test-friendly-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ 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 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"
))

expect_true(all(friendly_types(array(1:3)) == "an integer 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(matrix(letters)) == "a character matrix"))
expect_true(all(friendly_types(array(letters[1:3])) == "a character 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 array"
))
Expand Down
19 changes: 19 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,22 @@ 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"
))
})
})