Skip to content

Commit d0aaee9

Browse files
committed
better model summary code and examples
1 parent 4130745 commit d0aaee9

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

R/aaa_models.R

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,18 @@ get_model_env <- function() {
9090
#' @param value A list that conforms to the `fit_obj` or `pred_obj` description
9191
#' above, depending on context.
9292
#' @keywords internal
93+
#' @examples
94+
#' # Show the infomration about a model:
95+
#' show_model_info("rand_forest")
96+
#'
97+
#' # Access the model data:
98+
#'
99+
#' current_code <- get_model_env()
100+
#' ls(envir = current_code)
101+
#'
93102
#' @export
94103
check_mod_val <- function(model, new = FALSE, existence = FALSE) {
95-
if (is_missing(model) || length(model) != 1)
104+
if (rlang::is_missing(model) || length(model) != 1)
96105
stop("Please supply a character string for a model name (e.g. `'linear_reg'`)",
97106
call. = FALSE)
98107

@@ -120,7 +129,7 @@ check_mod_val <- function(model, new = FALSE, existence = FALSE) {
120129
#' @keywords internal
121130
#' @export
122131
check_mode_val <- function(mode) {
123-
if (is_missing(mode) || length(mode) != 1)
132+
if (rlang::is_missing(mode) || length(mode) != 1)
124133
stop("Please supply a character string for a mode (e.g. `'regression'`)",
125134
call. = FALSE)
126135
invisible(NULL)
@@ -130,7 +139,7 @@ check_mode_val <- function(mode) {
130139
#' @keywords internal
131140
#' @export
132141
check_engine_val <- function(eng) {
133-
if (is_missing(eng) || length(eng) != 1)
142+
if (rlang::is_missing(eng) || length(eng) != 1)
134143
stop("Please supply a character string for an engine (e.g. `'lm'`)",
135144
call. = FALSE)
136145
invisible(NULL)
@@ -140,7 +149,7 @@ check_engine_val <- function(eng) {
140149
#' @keywords internal
141150
#' @export
142151
check_arg_val <- function(arg) {
143-
if (is_missing(arg) || length(arg) != 1)
152+
if (rlang::is_missing(arg) || length(arg) != 1)
144153
stop("Please supply a character string for the argument",
145154
call. = FALSE)
146155
invisible(NULL)
@@ -166,7 +175,7 @@ check_func_val <- function(func) {
166175
"element 'pkg'. These should both be single character strings."
167176
)
168177

169-
if (is_missing(func) || !is.vector(func) || length(func) > 2)
178+
if (rlang::is_missing(func) || !is.vector(func) || length(func) > 2)
170179
stop(msg, call. = FALSE)
171180

172181
nms <- sort(names(func))
@@ -266,7 +275,7 @@ check_pred_info <- function(pred_obj, type) {
266275
#' @keywords internal
267276
#' @export
268277
check_pkg_val <- function(pkg) {
269-
if (is_missing(pkg) || length(pkg) != 1 || !is.character(pkg))
278+
if (rlang::is_missing(pkg) || length(pkg) != 1 || !is.character(pkg))
270279
stop("Please supply a single character vale for the package name",
271280
call. = FALSE)
272281
invisible(NULL)
@@ -609,7 +618,7 @@ show_model_info <- function(model) {
609618
cat(
610619
" modes:",
611620
paste0(current[[paste0(model, "_modes")]], collapse = ", "),
612-
"\n"
621+
"\n\n"
613622
)
614623

615624
engines <- current[[paste0(model)]]
@@ -629,6 +638,7 @@ show_model_info <- function(model) {
629638
dplyr::ungroup() %>%
630639
dplyr::pull(lab) %>%
631640
cat(sep = "")
641+
cat("\n")
632642
} else {
633643
cat(" no registered engines yet.")
634644
}
@@ -652,17 +662,40 @@ show_model_info <- function(model) {
652662
dplyr::ungroup() %>%
653663
dplyr::pull(lab) %>%
654664
cat(sep = "")
665+
cat("\n")
655666
} else {
656667
cat(" no registered arguments yet.")
657668
}
658669

659-
fits <- current[[paste0(model, "_fits")]]
670+
fits <- current[[paste0(model, "_fit")]]
660671
if (nrow(fits) > 0) {
661-
672+
cat(" fit modules:\n")
673+
fits %>%
674+
dplyr::select(-value) %>%
675+
mutate(engine = paste0(" ", engine)) %>%
676+
as.data.frame() %>%
677+
print(row.names = FALSE)
678+
cat("\n")
662679
} else {
663680
cat(" no registered fit modules yet.")
664681
}
665682

683+
preds <- current[[paste0(model, "_predict")]]
684+
if (nrow(preds) > 0) {
685+
cat(" prediction modules:\n")
686+
preds %>%
687+
dplyr::group_by(mode, engine) %>%
688+
dplyr::summarize(methods = paste0(sort(type), collapse = ", ")) %>%
689+
dplyr::ungroup() %>%
690+
mutate(mode = paste0(" ", mode)) %>%
691+
as.data.frame() %>%
692+
print(row.names = FALSE)
693+
cat("\n")
694+
} else {
695+
cat(" no registered prediction modules yet.")
696+
}
697+
698+
666699
invisible(NULL)
667700
}
668701

man/check_mod_val.Rd

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)