Skip to content

Commit d6e80c7

Browse files
committed
some documentation
1 parent edcddce commit d6e80c7

File tree

3 files changed

+172
-15
lines changed

3 files changed

+172
-15
lines changed

R/aaa_models.R

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Initialize model environment
1+
# Initialize model environments
22

33
# ------------------------------------------------------------------------------
44

@@ -35,20 +35,41 @@ pred_types <-
3535

3636
# ------------------------------------------------------------------------------
3737

38+
#' Tools to Register Models
39+
#'
40+
#' @keywords internal
3841
#' @export
3942
get_model_env <- function() {
4043
current <- utils::getFromNamespace("parsnip", ns = "parsnip")
4144
# current <- parsnip
4245
current
4346
}
4447

48+
49+
50+
#' Tools to Check Model Elements
51+
#'
52+
#' These functions are similar to constructors and can be used to validate
53+
#' that there are no conflicts with the underlying model structures used by the
54+
#' package.
55+
#'
56+
#' @param mod A single character string for the model type (e.g.
57+
#' `"rand_forest"`, etc).
58+
#' @param new A single logical to check to see if the model that you are check
59+
#' has not already been registered.
60+
#' @param existence A single logical to check to see if the model has already
61+
#' been registered.
62+
#' @param mode A single character string for the model mode (e.g. "regression").
63+
#' @param eng A single character string for the model engine.
64+
#' @param arg A single character string for the model argument name.
65+
#' @keywords internal
4566
#' @export
46-
check_mod_val <- function(mod, new = FALSE, existance = FALSE) {
67+
check_mod_val <- function(mod, new = FALSE, existence = FALSE) {
4768
if (is_missing(mod) || length(mod) != 1)
4869
stop("Please supply a character string for a model name (e.g. `'linear_reg'`)",
4970
call. = FALSE)
5071

51-
if (new | existance) {
72+
if (new | existence) {
5273
current <- get_model_env()
5374
}
5475

@@ -58,7 +79,7 @@ check_mod_val <- function(mod, new = FALSE, existance = FALSE) {
5879
}
5980
}
6081

61-
if (existance) {
82+
if (existence) {
6283
current <- get_model_env()
6384
if (!any(current$models == mod)) {
6485
stop("Model `", mod, "` has not been registered.", call. = FALSE)
@@ -68,6 +89,8 @@ check_mod_val <- function(mod, new = FALSE, existance = FALSE) {
6889
invisible(NULL)
6990
}
7091

92+
#' @rdname check_mod_val
93+
#' @keywords internal
7194
#' @export
7295
check_mode_val <- function(mode) {
7396
if (is_missing(mode) || length(mode) != 1)
@@ -76,6 +99,8 @@ check_mode_val <- function(mode) {
7699
invisible(NULL)
77100
}
78101

102+
#' @rdname check_mod_val
103+
#' @keywords internal
79104
#' @export
80105
check_engine_val <- function(eng) {
81106
if (is_missing(eng) || length(eng) != 1)
@@ -84,6 +109,8 @@ check_engine_val <- function(eng) {
84109
invisible(NULL)
85110
}
86111

112+
#' @rdname check_mod_val
113+
#' @keywords internal
87114
#' @export
88115
check_arg_val <- function(arg) {
89116
if (is_missing(arg) || length(arg) != 1)
@@ -92,6 +119,8 @@ check_arg_val <- function(arg) {
92119
invisible(NULL)
93120
}
94121

122+
#' @rdname check_mod_val
123+
#' @keywords internal
95124
#' @export
96125
check_submodels_val <- function(x) {
97126
if (!is.logical(x) || length(x) != 1) {
@@ -100,6 +129,8 @@ check_submodels_val <- function(x) {
100129
invisible(NULL)
101130
}
102131

132+
#' @rdname check_mod_val
133+
#' @keywords internal
103134
#' @export
104135
check_func_val <- function(func) {
105136
msg <-
@@ -135,6 +166,8 @@ check_func_val <- function(func) {
135166
invisible(NULL)
136167
}
137168

169+
#' @rdname check_mod_val
170+
#' @keywords internal
138171
#' @export
139172
check_fit_info <- function(x) {
140173
if (is.null(x)) {
@@ -167,6 +200,8 @@ check_fit_info <- function(x) {
167200
invisible(NULL)
168201
}
169202

203+
#' @rdname check_mod_val
204+
#' @keywords internal
170205
#' @export
171206
check_pred_info <- function(x, type) {
172207
if (all(type != pred_types)) {
@@ -200,7 +235,8 @@ check_pred_info <- function(x, type) {
200235
invisible(NULL)
201236
}
202237

203-
238+
#' @rdname check_mod_val
239+
#' @keywords internal
204240
#' @export
205241
check_pkg_val <- function(x) {
206242
if (is_missing(x) || length(x) != 1 || !is.character(x))
@@ -211,6 +247,8 @@ check_pkg_val <- function(x) {
211247

212248
# ------------------------------------------------------------------------------
213249

250+
#' @rdname get_model_env
251+
#' @keywords internal
214252
#' @export
215253
set_new_model <- function(mod) {
216254
check_mod_val(mod, new = TRUE)
@@ -247,9 +285,11 @@ set_new_model <- function(mod) {
247285

248286
# ------------------------------------------------------------------------------
249287

288+
#' @rdname get_model_env
289+
#' @keywords internal
250290
#' @export
251291
set_model_mode <- function(mod, mode) {
252-
check_mod_val(mod, existance = TRUE)
292+
check_mod_val(mod, existence = TRUE)
253293
check_mode_val(mode)
254294

255295
current <- get_model_env()
@@ -265,9 +305,11 @@ set_model_mode <- function(mod, mode) {
265305

266306
# ------------------------------------------------------------------------------
267307

308+
#' @rdname get_model_env
309+
#' @keywords internal
268310
#' @export
269311
set_model_engine <- function(mod, mode, eng) {
270-
check_mod_val(mod, existance = TRUE)
312+
check_mod_val(mod, existence = TRUE)
271313
check_mode_val(mode)
272314
check_mode_val(eng)
273315

@@ -288,9 +330,11 @@ set_model_engine <- function(mod, mode, eng) {
288330

289331
# ------------------------------------------------------------------------------
290332

333+
#' @rdname get_model_env
334+
#' @keywords internal
291335
#' @export
292336
set_model_arg <- function(mod, eng, val, original, func, submodels) {
293-
check_mod_val(mod, existance = TRUE)
337+
check_mod_val(mod, existence = TRUE)
294338
check_arg_val(val)
295339
check_arg_val(original)
296340
check_func_val(func)
@@ -325,9 +369,11 @@ set_model_arg <- function(mod, eng, val, original, func, submodels) {
325369

326370
# ------------------------------------------------------------------------------
327371

372+
#' @rdname get_model_env
373+
#' @keywords internal
328374
#' @export
329375
set_dependency <- function(mod, eng, pkg) {
330-
check_mod_val(mod, existance = TRUE)
376+
check_mod_val(mod, existence = TRUE)
331377
check_pkg_val(pkg)
332378

333379
current <- get_model_env()
@@ -366,9 +412,11 @@ set_dependency <- function(mod, eng, pkg) {
366412
invisible(NULL)
367413
}
368414

415+
#' @rdname get_model_env
416+
#' @keywords internal
369417
#' @export
370418
get_dependency <- function(mod) {
371-
check_mod_val(mod, existance = TRUE)
419+
check_mod_val(mod, existence = TRUE)
372420
pkg_name <- paste0(mod, "_pkgs")
373421
if (!any(pkg_name != rlang::env_names(get_model_env()))) {
374422
stop("`", mod, "` does not have a dependency list in parsnip.", call. = FALSE)
@@ -379,9 +427,11 @@ get_dependency <- function(mod) {
379427

380428
# ------------------------------------------------------------------------------
381429

430+
#' @rdname get_model_env
431+
#' @keywords internal
382432
#' @export
383433
set_fit <- function(mod, mode, eng, value) {
384-
check_mod_val(mod, existance = TRUE)
434+
check_mod_val(mod, existence = TRUE)
385435
check_mode_val(mode)
386436
check_engine_val(eng)
387437
check_fit_info(value)
@@ -428,9 +478,11 @@ set_fit <- function(mod, mode, eng, value) {
428478
invisible(NULL)
429479
}
430480

481+
#' @rdname get_model_env
482+
#' @keywords internal
431483
#' @export
432484
get_fit <- function(mod) {
433-
check_mod_val(mod, existance = TRUE)
485+
check_mod_val(mod, existence = TRUE)
434486
fit_name <- paste0(mod, "_fit")
435487
if (!any(fit_name != rlang::env_names(get_model_env()))) {
436488
stop("`", mod, "` does not have a `fit` method in parsnip.", call. = FALSE)
@@ -440,9 +492,11 @@ get_fit <- function(mod) {
440492

441493
# ------------------------------------------------------------------------------
442494

495+
#' @rdname get_model_env
496+
#' @keywords internal
443497
#' @export
444498
set_pred <- function(mod, mode, eng, type, value) {
445-
check_mod_val(mod, existance = TRUE)
499+
check_mod_val(mod, existence = TRUE)
446500
check_mode_val(mode)
447501
check_engine_val(eng)
448502
check_pred_info(value, type)
@@ -490,9 +544,11 @@ set_pred <- function(mod, mode, eng, type, value) {
490544
invisible(NULL)
491545
}
492546

547+
#' @rdname get_model_env
548+
#' @keywords internal
493549
#' @export
494550
get_pred_type <- function(mod, type) {
495-
check_mod_val(mod, existance = TRUE)
551+
check_mod_val(mod, existence = TRUE)
496552
pred_name <- paste0(mod, "_predict")
497553
if (!any(pred_name != rlang::env_names(get_model_env()))) {
498554
stop("`", mod, "` does not have any `pred` methods in parsnip.", call. = FALSE)
@@ -514,9 +570,11 @@ validate_model <- function(mod) {
514570

515571
# ------------------------------------------------------------------------------
516572

573+
#' @rdname get_model_env
574+
#' @keywords internal
517575
#' @export
518576
show_model_info <- function(mod) {
519-
check_mod_val(mod, existance = TRUE)
577+
check_mod_val(mod, existence = TRUE)
520578
current <- get_model_env()
521579

522580
cat("Information for `", mod, "`\n", sep = "")

man/check_mod_val.Rd

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

man/get_model_env.Rd

Lines changed: 45 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)