| 1 |
#' Tidying methods for lists / returned values that are not S3 objects |
| 2 |
#' |
| 3 |
#' Broom tidies a number of lists that are effectively S3 objects without |
| 4 |
#' a class attribute. For example, [stats::optim()], [base::svd()] and |
| 5 |
#' [interp::interp()] produce consistent output, but because they do not |
| 6 |
#' have a class attribute, they cannot be handled by S3 dispatch. |
| 7 |
#' |
| 8 |
#' These functions look at the elements of a list and determine if there is |
| 9 |
#' an appropriate tidying method to apply to the list. Those tidiers are |
| 10 |
#' themselves are implemented as functions of the form `tidy_<function>` |
| 11 |
#' or `glance_<function>` and are not exported (but they are documented!). |
| 12 |
#' |
| 13 |
#' If no appropriate tidying method is found, throws an error. |
| 14 |
#' |
| 15 |
#' @param x A list, potentially representing an object that can be tidied. |
| 16 |
#' @param ... Additionally, arguments passed to the tidying function. |
| 17 |
#' |
| 18 |
#' @name list_tidiers |
| 19 |
#' @export |
| 20 |
#' @family list tidiers |
| 21 |
tidy.list <- function(x, ...) { |
| 22 |
optim_elems <- c("par", "value", "counts", "convergence", "message") |
| 23 |
xyz_elems <- c("x", "y", "z") |
| 24 |
svd_elems <- c("d", "u", "v") |
| 25 |
irlba_elems <- c(svd_elems, "iter", "mprod") |
| 26 |
|
| 27 |
if (all(optim_elems %in% names(x))) { |
| 28 |
tidy_optim(x, ...) |
| 29 |
} else if (all(xyz_elems %in% names(x))) { |
| 30 |
tidy_xyz(x, ...) |
| 31 |
} else if (all(irlba_elems %in% names(x))) { |
| 32 |
tidy_irlba(x, ...) |
| 33 |
} else if (all(svd_elems %in% names(x))) { |
| 34 |
tidy_svd(x, ...) |
| 35 |
} else { |
| 36 |
cli::cli_abort("No {.fn tidy} method recognized for this list.") |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
#' @rdname list_tidiers |
| 41 |
#' @export |
| 42 |
glance.list <- function(x, ...) { |
| 43 |
optim_elems <- c("par", "value", "counts", "convergence", "message") |
| 44 |
|
| 45 |
if (all(optim_elems %in% names(x))) { |
| 46 |
glance_optim(x, ...) |
| 47 |
} else { |
| 48 |
cli::cli_abort("No {.fn glance} method recognized for this list.") |
| 49 |
} |
| 50 |
} |