1
- # Initialize model environment
1
+ # Initialize model environments
2
2
3
3
# ------------------------------------------------------------------------------
4
4
@@ -35,20 +35,41 @@ pred_types <-
35
35
36
36
# ------------------------------------------------------------------------------
37
37
38
+ # ' Tools to Register Models
39
+ # '
40
+ # ' @keywords internal
38
41
# ' @export
39
42
get_model_env <- function () {
40
43
current <- utils :: getFromNamespace(" parsnip" , ns = " parsnip" )
41
44
# current <- parsnip
42
45
current
43
46
}
44
47
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
45
66
# ' @export
46
- check_mod_val <- function (mod , new = FALSE , existance = FALSE ) {
67
+ check_mod_val <- function (mod , new = FALSE , existence = FALSE ) {
47
68
if (is_missing(mod ) || length(mod ) != 1 )
48
69
stop(" Please supply a character string for a model name (e.g. `'linear_reg'`)" ,
49
70
call. = FALSE )
50
71
51
- if (new | existance ) {
72
+ if (new | existence ) {
52
73
current <- get_model_env()
53
74
}
54
75
@@ -58,7 +79,7 @@ check_mod_val <- function(mod, new = FALSE, existance = FALSE) {
58
79
}
59
80
}
60
81
61
- if (existance ) {
82
+ if (existence ) {
62
83
current <- get_model_env()
63
84
if (! any(current $ models == mod )) {
64
85
stop(" Model `" , mod , " ` has not been registered." , call. = FALSE )
@@ -68,6 +89,8 @@ check_mod_val <- function(mod, new = FALSE, existance = FALSE) {
68
89
invisible (NULL )
69
90
}
70
91
92
+ # ' @rdname check_mod_val
93
+ # ' @keywords internal
71
94
# ' @export
72
95
check_mode_val <- function (mode ) {
73
96
if (is_missing(mode ) || length(mode ) != 1 )
@@ -76,6 +99,8 @@ check_mode_val <- function(mode) {
76
99
invisible (NULL )
77
100
}
78
101
102
+ # ' @rdname check_mod_val
103
+ # ' @keywords internal
79
104
# ' @export
80
105
check_engine_val <- function (eng ) {
81
106
if (is_missing(eng ) || length(eng ) != 1 )
@@ -84,6 +109,8 @@ check_engine_val <- function(eng) {
84
109
invisible (NULL )
85
110
}
86
111
112
+ # ' @rdname check_mod_val
113
+ # ' @keywords internal
87
114
# ' @export
88
115
check_arg_val <- function (arg ) {
89
116
if (is_missing(arg ) || length(arg ) != 1 )
@@ -92,6 +119,8 @@ check_arg_val <- function(arg) {
92
119
invisible (NULL )
93
120
}
94
121
122
+ # ' @rdname check_mod_val
123
+ # ' @keywords internal
95
124
# ' @export
96
125
check_submodels_val <- function (x ) {
97
126
if (! is.logical(x ) || length(x ) != 1 ) {
@@ -100,6 +129,8 @@ check_submodels_val <- function(x) {
100
129
invisible (NULL )
101
130
}
102
131
132
+ # ' @rdname check_mod_val
133
+ # ' @keywords internal
103
134
# ' @export
104
135
check_func_val <- function (func ) {
105
136
msg <-
@@ -135,6 +166,8 @@ check_func_val <- function(func) {
135
166
invisible (NULL )
136
167
}
137
168
169
+ # ' @rdname check_mod_val
170
+ # ' @keywords internal
138
171
# ' @export
139
172
check_fit_info <- function (x ) {
140
173
if (is.null(x )) {
@@ -167,6 +200,8 @@ check_fit_info <- function(x) {
167
200
invisible (NULL )
168
201
}
169
202
203
+ # ' @rdname check_mod_val
204
+ # ' @keywords internal
170
205
# ' @export
171
206
check_pred_info <- function (x , type ) {
172
207
if (all(type != pred_types )) {
@@ -200,7 +235,8 @@ check_pred_info <- function(x, type) {
200
235
invisible (NULL )
201
236
}
202
237
203
-
238
+ # ' @rdname check_mod_val
239
+ # ' @keywords internal
204
240
# ' @export
205
241
check_pkg_val <- function (x ) {
206
242
if (is_missing(x ) || length(x ) != 1 || ! is.character(x ))
@@ -211,6 +247,8 @@ check_pkg_val <- function(x) {
211
247
212
248
# ------------------------------------------------------------------------------
213
249
250
+ # ' @rdname get_model_env
251
+ # ' @keywords internal
214
252
# ' @export
215
253
set_new_model <- function (mod ) {
216
254
check_mod_val(mod , new = TRUE )
@@ -247,9 +285,11 @@ set_new_model <- function(mod) {
247
285
248
286
# ------------------------------------------------------------------------------
249
287
288
+ # ' @rdname get_model_env
289
+ # ' @keywords internal
250
290
# ' @export
251
291
set_model_mode <- function (mod , mode ) {
252
- check_mod_val(mod , existance = TRUE )
292
+ check_mod_val(mod , existence = TRUE )
253
293
check_mode_val(mode )
254
294
255
295
current <- get_model_env()
@@ -265,9 +305,11 @@ set_model_mode <- function(mod, mode) {
265
305
266
306
# ------------------------------------------------------------------------------
267
307
308
+ # ' @rdname get_model_env
309
+ # ' @keywords internal
268
310
# ' @export
269
311
set_model_engine <- function (mod , mode , eng ) {
270
- check_mod_val(mod , existance = TRUE )
312
+ check_mod_val(mod , existence = TRUE )
271
313
check_mode_val(mode )
272
314
check_mode_val(eng )
273
315
@@ -288,9 +330,11 @@ set_model_engine <- function(mod, mode, eng) {
288
330
289
331
# ------------------------------------------------------------------------------
290
332
333
+ # ' @rdname get_model_env
334
+ # ' @keywords internal
291
335
# ' @export
292
336
set_model_arg <- function (mod , eng , val , original , func , submodels ) {
293
- check_mod_val(mod , existance = TRUE )
337
+ check_mod_val(mod , existence = TRUE )
294
338
check_arg_val(val )
295
339
check_arg_val(original )
296
340
check_func_val(func )
@@ -325,9 +369,11 @@ set_model_arg <- function(mod, eng, val, original, func, submodels) {
325
369
326
370
# ------------------------------------------------------------------------------
327
371
372
+ # ' @rdname get_model_env
373
+ # ' @keywords internal
328
374
# ' @export
329
375
set_dependency <- function (mod , eng , pkg ) {
330
- check_mod_val(mod , existance = TRUE )
376
+ check_mod_val(mod , existence = TRUE )
331
377
check_pkg_val(pkg )
332
378
333
379
current <- get_model_env()
@@ -366,9 +412,11 @@ set_dependency <- function(mod, eng, pkg) {
366
412
invisible (NULL )
367
413
}
368
414
415
+ # ' @rdname get_model_env
416
+ # ' @keywords internal
369
417
# ' @export
370
418
get_dependency <- function (mod ) {
371
- check_mod_val(mod , existance = TRUE )
419
+ check_mod_val(mod , existence = TRUE )
372
420
pkg_name <- paste0(mod , " _pkgs" )
373
421
if (! any(pkg_name != rlang :: env_names(get_model_env()))) {
374
422
stop(" `" , mod , " ` does not have a dependency list in parsnip." , call. = FALSE )
@@ -379,9 +427,11 @@ get_dependency <- function(mod) {
379
427
380
428
# ------------------------------------------------------------------------------
381
429
430
+ # ' @rdname get_model_env
431
+ # ' @keywords internal
382
432
# ' @export
383
433
set_fit <- function (mod , mode , eng , value ) {
384
- check_mod_val(mod , existance = TRUE )
434
+ check_mod_val(mod , existence = TRUE )
385
435
check_mode_val(mode )
386
436
check_engine_val(eng )
387
437
check_fit_info(value )
@@ -428,9 +478,11 @@ set_fit <- function(mod, mode, eng, value) {
428
478
invisible (NULL )
429
479
}
430
480
481
+ # ' @rdname get_model_env
482
+ # ' @keywords internal
431
483
# ' @export
432
484
get_fit <- function (mod ) {
433
- check_mod_val(mod , existance = TRUE )
485
+ check_mod_val(mod , existence = TRUE )
434
486
fit_name <- paste0(mod , " _fit" )
435
487
if (! any(fit_name != rlang :: env_names(get_model_env()))) {
436
488
stop(" `" , mod , " ` does not have a `fit` method in parsnip." , call. = FALSE )
@@ -440,9 +492,11 @@ get_fit <- function(mod) {
440
492
441
493
# ------------------------------------------------------------------------------
442
494
495
+ # ' @rdname get_model_env
496
+ # ' @keywords internal
443
497
# ' @export
444
498
set_pred <- function (mod , mode , eng , type , value ) {
445
- check_mod_val(mod , existance = TRUE )
499
+ check_mod_val(mod , existence = TRUE )
446
500
check_mode_val(mode )
447
501
check_engine_val(eng )
448
502
check_pred_info(value , type )
@@ -490,9 +544,11 @@ set_pred <- function(mod, mode, eng, type, value) {
490
544
invisible (NULL )
491
545
}
492
546
547
+ # ' @rdname get_model_env
548
+ # ' @keywords internal
493
549
# ' @export
494
550
get_pred_type <- function (mod , type ) {
495
- check_mod_val(mod , existance = TRUE )
551
+ check_mod_val(mod , existence = TRUE )
496
552
pred_name <- paste0(mod , " _predict" )
497
553
if (! any(pred_name != rlang :: env_names(get_model_env()))) {
498
554
stop(" `" , mod , " ` does not have any `pred` methods in parsnip." , call. = FALSE )
@@ -514,9 +570,11 @@ validate_model <- function(mod) {
514
570
515
571
# ------------------------------------------------------------------------------
516
572
573
+ # ' @rdname get_model_env
574
+ # ' @keywords internal
517
575
# ' @export
518
576
show_model_info <- function (mod ) {
519
- check_mod_val(mod , existance = TRUE )
577
+ check_mod_val(mod , existence = TRUE )
520
578
current <- get_model_env()
521
579
522
580
cat(" Information for `" , mod , " `\n " , sep = " " )
0 commit comments