| 1 |
#' @templateVar class xyz |
| 2 |
#' @template title_desc_tidy_list |
| 3 |
#' |
| 4 |
#' @description xyz lists (lists where `x` and `y` are vectors of coordinates |
| 5 |
#' and `z` is a matrix of values) are typically used by functions such as |
| 6 |
#' [graphics::persp()] or [graphics::image()] and returned |
| 7 |
#' by interpolation functions such as [interp::interp()]. |
| 8 |
#' |
| 9 |
#' @param x A list with component `x`, `y` and `z`, where `x` and `y` are |
| 10 |
#' vectors and `z` is a matrix. The length of `x` must equal the number of |
| 11 |
#' rows in `z` and the length of `y` must equal the number of columns in `z`. |
| 12 |
#' @template param_unused_dots |
| 13 |
#' |
| 14 |
#' @return A [tibble::tibble] with vector columns `x`, `y` and `z`. |
| 15 |
#' |
| 16 |
#' @examples |
| 17 |
#' |
| 18 |
#' A <- list(x = 1:5, y = 1:3, z = matrix(runif(5 * 3), nrow = 5)) |
| 19 |
#' image(A) |
| 20 |
#' tidy(A) |
| 21 |
#' @aliases xyz_tidiers |
| 22 |
#' @family list tidiers |
| 23 |
#' @seealso [tidy()], [graphics::persp()], [graphics::image()], |
| 24 |
#' [interp::interp()] |
| 25 |
#' |
| 26 |
tidy_xyz <- function(x, ...) { |
| 27 |
if (!is.matrix(x$z)) { |
| 28 |
cli::cli_abort("{.arg z} must be a matrix.") |
| 29 |
} |
| 30 |
|
| 31 |
if (length(x$x) != nrow(x$z) || length(x$y) != ncol(x$z)) { |
| 32 |
cli::cli_abort(c( |
| 33 |
"To tidy an xyz list, the length of element {.code x} must equal the |
| 34 |
number of rows of element {.code z}, and the length of element |
| 35 |
{.code y} must equal the number of columns of element {.code z}." |
| 36 |
)) |
| 37 |
} |
| 38 |
|
| 39 |
as_tibble(data.frame( |
| 40 |
x = x$x, |
| 41 |
y = rep(x$y, each = length(x$x)), |
| 42 |
z = as.numeric(x$z) |
| 43 |
)) |
| 44 |
} |