@@ -53,7 +53,7 @@ get_model_env <- function() {
53
53
# ' that there are no conflicts with the underlying model structures used by the
54
54
# ' package.
55
55
# '
56
- # ' @param mod A single character string for the model type (e.g.
56
+ # ' @param model A single character string for the model type (e.g.
57
57
# ' `"rand_forest"`, etc).
58
58
# ' @param new A single logical to check to see if the model that you are check
59
59
# ' has not already been registered.
@@ -64,8 +64,8 @@ get_model_env <- function() {
64
64
# ' @param arg A single character string for the model argument name.
65
65
# ' @keywords internal
66
66
# ' @export
67
- check_mod_val <- function (mod , new = FALSE , existence = FALSE ) {
68
- if (is_missing(mod ) || length(mod ) != 1 )
67
+ check_mod_val <- function (model , new = FALSE , existence = FALSE ) {
68
+ if (is_missing(model ) || length(model ) != 1 )
69
69
stop(" Please supply a character string for a model name (e.g. `'linear_reg'`)" ,
70
70
call. = FALSE )
71
71
@@ -74,15 +74,15 @@ check_mod_val <- function(mod, new = FALSE, existence = FALSE) {
74
74
}
75
75
76
76
if (new ) {
77
- if (any(current $ models == mod )) {
78
- stop(" Model `" , mod , " ` already exists" , call. = FALSE )
77
+ if (any(current $ models == model )) {
78
+ stop(" Model `" , model , " ` already exists" , call. = FALSE )
79
79
}
80
80
}
81
81
82
82
if (existence ) {
83
83
current <- get_model_env()
84
- if (! any(current $ models == mod )) {
85
- stop(" Model `" , mod , " ` has not been registered." , call. = FALSE )
84
+ if (! any(current $ models == model )) {
85
+ stop(" Model `" , model , " ` has not been registered." , call. = FALSE )
86
86
}
87
87
}
88
88
@@ -250,29 +250,29 @@ check_pkg_val <- function(x) {
250
250
# ' @rdname get_model_env
251
251
# ' @keywords internal
252
252
# ' @export
253
- set_new_model <- function (mod ) {
254
- check_mod_val(mod , new = TRUE )
253
+ set_new_model <- function (model ) {
254
+ check_mod_val(model , new = TRUE )
255
255
256
256
current <- get_model_env()
257
257
258
- current $ models <- c(current $ models , mod )
259
- current [[mod ]] <- dplyr :: tibble(engine = character (0 ), mode = character (0 ))
260
- current [[paste0(mod , " _pkgs" )]] <- dplyr :: tibble(engine = character (0 ), pkg = list ())
261
- current [[paste0(mod , " _modes" )]] <- " unknown"
262
- current [[paste0(mod , " _args" )]] <-
258
+ current $ models <- c(current $ models , model )
259
+ current [[model ]] <- dplyr :: tibble(engine = character (0 ), mode = character (0 ))
260
+ current [[paste0(model , " _pkgs" )]] <- dplyr :: tibble(engine = character (0 ), pkg = list ())
261
+ current [[paste0(model , " _modes" )]] <- " unknown"
262
+ current [[paste0(model , " _args" )]] <-
263
263
dplyr :: tibble(
264
264
engine = character (0 ),
265
265
parsnip = character (0 ),
266
266
original = character (0 ),
267
267
func = list ()
268
268
)
269
- current [[paste0(mod , " _fit" )]] <-
269
+ current [[paste0(model , " _fit" )]] <-
270
270
dplyr :: tibble(
271
271
engine = character (0 ),
272
272
mode = character (0 ),
273
273
value = list ()
274
274
)
275
- current [[paste0(mod , " _predict" )]] <-
275
+ current [[paste0(model , " _predict" )]] <-
276
276
dplyr :: tibble(
277
277
engine = character (0 ),
278
278
mode = character (0 ),
@@ -288,17 +288,17 @@ set_new_model <- function(mod) {
288
288
# ' @rdname get_model_env
289
289
# ' @keywords internal
290
290
# ' @export
291
- set_model_mode <- function (mod , mode ) {
292
- check_mod_val(mod , existence = TRUE )
291
+ set_model_mode <- function (model , mode ) {
292
+ check_mod_val(model , existence = TRUE )
293
293
check_mode_val(mode )
294
294
295
295
current <- get_model_env()
296
296
297
297
if (! any(current $ modes == mode )) {
298
298
current $ modes <- unique(c(current $ modes , mode ))
299
299
}
300
- current [[paste0(mod , " _modes" )]] <-
301
- unique(c(current [[paste0(mod , " _modes" )]], mode ))
300
+ current [[paste0(model , " _modes" )]] <-
301
+ unique(c(current [[paste0(model , " _modes" )]], mode ))
302
302
303
303
invisible (NULL )
304
304
}
@@ -308,21 +308,21 @@ set_model_mode <- function(mod, mode) {
308
308
# ' @rdname get_model_env
309
309
# ' @keywords internal
310
310
# ' @export
311
- set_model_engine <- function (mod , mode , eng ) {
312
- check_mod_val(mod , existence = TRUE )
311
+ set_model_engine <- function (model , mode , eng ) {
312
+ check_mod_val(model , existence = TRUE )
313
313
check_mode_val(mode )
314
314
check_mode_val(eng )
315
315
316
316
current <- get_model_env()
317
317
318
318
new_eng <- dplyr :: tibble(engine = eng , mode = mode )
319
- old_eng <- current [[mod ]]
319
+ old_eng <- current [[model ]]
320
320
engs <-
321
321
old_eng %> %
322
322
dplyr :: bind_rows(new_eng ) %> %
323
323
dplyr :: distinct()
324
324
325
- current [[mod ]] <- engs
325
+ current [[model ]] <- engs
326
326
327
327
invisible (NULL )
328
328
}
@@ -333,15 +333,15 @@ set_model_engine <- function(mod, mode, eng) {
333
333
# ' @rdname get_model_env
334
334
# ' @keywords internal
335
335
# ' @export
336
- set_model_arg <- function (mod , eng , val , original , func , submodels ) {
337
- check_mod_val(mod , existence = TRUE )
336
+ set_model_arg <- function (model , eng , val , original , func , submodels ) {
337
+ check_mod_val(model , existence = TRUE )
338
338
check_arg_val(val )
339
339
check_arg_val(original )
340
340
check_func_val(func )
341
341
check_submodels_val(submodels )
342
342
343
343
current <- get_model_env()
344
- old_args <- current [[paste0(mod , " _args" )]]
344
+ old_args <- current [[paste0(model , " _args" )]]
345
345
346
346
new_arg <-
347
347
dplyr :: tibble(
@@ -361,7 +361,7 @@ set_model_arg <- function(mod, eng, val, original, func, submodels) {
361
361
362
362
updated <- dplyr :: distinct(updated , engine , parsnip , original , submodels )
363
363
364
- current [[paste0(mod , " _args" )]] <- updated
364
+ current [[paste0(model , " _args" )]] <- updated
365
365
366
366
invisible (NULL )
367
367
}
@@ -372,13 +372,13 @@ set_model_arg <- function(mod, eng, val, original, func, submodels) {
372
372
# ' @rdname get_model_env
373
373
# ' @keywords internal
374
374
# ' @export
375
- set_dependency <- function (mod , eng , pkg ) {
376
- check_mod_val(mod , existence = TRUE )
375
+ set_dependency <- function (model , eng , pkg ) {
376
+ check_mod_val(model , existence = TRUE )
377
377
check_pkg_val(pkg )
378
378
379
379
current <- get_model_env()
380
- model_info <- current [[mod ]]
381
- pkg_info <- current [[paste0(mod , " _pkgs" )]]
380
+ model_info <- current [[model ]]
381
+ pkg_info <- current [[paste0(model , " _pkgs" )]]
382
382
383
383
has_engine <-
384
384
model_info %> %
@@ -387,7 +387,7 @@ set_dependency <- function(mod, eng, pkg) {
387
387
nrow()
388
388
if (has_engine != 1 ) {
389
389
stop(" The engine '" , eng , " ' has not been registered for model '" ,
390
- mod , " '. " , call. = FALSE )
390
+ model , " '. " , call. = FALSE )
391
391
}
392
392
393
393
existing_pkgs <-
@@ -407,19 +407,19 @@ set_dependency <- function(mod, eng, pkg) {
407
407
dplyr :: filter(engine != eng ) %> %
408
408
dplyr :: bind_rows(existing_pkgs )
409
409
}
410
- current [[paste0(mod , " _pkgs" )]] <- pkg_info
410
+ current [[paste0(model , " _pkgs" )]] <- pkg_info
411
411
412
412
invisible (NULL )
413
413
}
414
414
415
415
# ' @rdname get_model_env
416
416
# ' @keywords internal
417
417
# ' @export
418
- get_dependency <- function (mod ) {
419
- check_mod_val(mod , existence = TRUE )
420
- pkg_name <- paste0(mod , " _pkgs" )
418
+ get_dependency <- function (model ) {
419
+ check_mod_val(model , existence = TRUE )
420
+ pkg_name <- paste0(model , " _pkgs" )
421
421
if (! any(pkg_name != rlang :: env_names(get_model_env()))) {
422
- stop(" `" , mod , " ` does not have a dependency list in parsnip." , call. = FALSE )
422
+ stop(" `" , model , " ` does not have a dependency list in parsnip." , call. = FALSE )
423
423
}
424
424
rlang :: env_get(get_model_env(), pkg_name )
425
425
}
@@ -430,15 +430,15 @@ get_dependency <- function(mod) {
430
430
# ' @rdname get_model_env
431
431
# ' @keywords internal
432
432
# ' @export
433
- set_fit <- function (mod , mode , eng , value ) {
434
- check_mod_val(mod , existence = TRUE )
433
+ set_fit <- function (model , mode , eng , value ) {
434
+ check_mod_val(model , existence = TRUE )
435
435
check_mode_val(mode )
436
436
check_engine_val(eng )
437
437
check_fit_info(value )
438
438
439
439
current <- get_model_env()
440
- model_info <- current [[paste0(mod )]]
441
- old_fits <- current [[paste0(mod , " _fit" )]]
440
+ model_info <- current [[paste0(model )]]
441
+ old_fits <- current [[paste0(model , " _fit" )]]
442
442
443
443
has_engine <-
444
444
model_info %> %
@@ -447,7 +447,7 @@ set_fit <- function(mod, mode, eng, value) {
447
447
if (has_engine != 1 ) {
448
448
stop(" set_fit The combination of engine '" , eng , " ' and mode '" ,
449
449
mode , " ' has not been registered for model '" ,
450
- mod , " '. " , call. = FALSE )
450
+ model , " '. " , call. = FALSE )
451
451
}
452
452
453
453
has_fit <-
@@ -458,7 +458,7 @@ set_fit <- function(mod, mode, eng, value) {
458
458
if (has_fit > 0 ) {
459
459
stop(" The combination of engine '" , eng , " ' and mode '" ,
460
460
mode , " ' already has a fit component for model '" ,
461
- mod , " '. " , call. = FALSE )
461
+ model , " '. " , call. = FALSE )
462
462
}
463
463
464
464
new_fit <-
@@ -473,19 +473,19 @@ set_fit <- function(mod, mode, eng, value) {
473
473
stop(" An error occured when adding the new fit module" , call. = FALSE )
474
474
}
475
475
476
- current [[paste0(mod , " _fit" )]] <- updated
476
+ current [[paste0(model , " _fit" )]] <- updated
477
477
478
478
invisible (NULL )
479
479
}
480
480
481
481
# ' @rdname get_model_env
482
482
# ' @keywords internal
483
483
# ' @export
484
- get_fit <- function (mod ) {
485
- check_mod_val(mod , existence = TRUE )
486
- fit_name <- paste0(mod , " _fit" )
484
+ get_fit <- function (model ) {
485
+ check_mod_val(model , existence = TRUE )
486
+ fit_name <- paste0(model , " _fit" )
487
487
if (! any(fit_name != rlang :: env_names(get_model_env()))) {
488
- stop(" `" , mod , " ` does not have a `fit` method in parsnip." , call. = FALSE )
488
+ stop(" `" , model , " ` does not have a `fit` method in parsnip." , call. = FALSE )
489
489
}
490
490
rlang :: env_get(get_model_env(), fit_name )
491
491
}
@@ -495,15 +495,15 @@ get_fit <- function(mod) {
495
495
# ' @rdname get_model_env
496
496
# ' @keywords internal
497
497
# ' @export
498
- set_pred <- function (mod , mode , eng , type , value ) {
499
- check_mod_val(mod , existence = TRUE )
498
+ set_pred <- function (model , mode , eng , type , value ) {
499
+ check_mod_val(model , existence = TRUE )
500
500
check_mode_val(mode )
501
501
check_engine_val(eng )
502
502
check_pred_info(value , type )
503
503
504
504
current <- get_model_env()
505
- model_info <- current [[paste0(mod )]]
506
- old_fits <- current [[paste0(mod , " _predict" )]]
505
+ model_info <- current [[paste0(model )]]
506
+ old_fits <- current [[paste0(model , " _predict" )]]
507
507
508
508
has_engine <-
509
509
model_info %> %
@@ -512,7 +512,7 @@ set_pred <- function(mod, mode, eng, type, value) {
512
512
if (has_engine != 1 ) {
513
513
stop(" The combination of engine '" , eng , " ' and mode '" ,
514
514
mode , " ' has not been registered for model '" ,
515
- mod , " '. " , call. = FALSE )
515
+ model , " '. " , call. = FALSE )
516
516
}
517
517
518
518
has_pred <-
@@ -523,7 +523,7 @@ set_pred <- function(mod, mode, eng, type, value) {
523
523
stop(" The combination of engine '" , eng , " ', mode '" ,
524
524
mode , " ', and type '" , type ,
525
525
" ' already has a prediction component for model '" ,
526
- mod , " '. " , call. = FALSE )
526
+ model , " '. " , call. = FALSE )
527
527
}
528
528
529
529
new_fit <-
@@ -539,23 +539,23 @@ set_pred <- function(mod, mode, eng, type, value) {
539
539
stop(" An error occured when adding the new fit module" , call. = FALSE )
540
540
}
541
541
542
- current [[paste0(mod , " _predict" )]] <- updated
542
+ current [[paste0(model , " _predict" )]] <- updated
543
543
544
544
invisible (NULL )
545
545
}
546
546
547
547
# ' @rdname get_model_env
548
548
# ' @keywords internal
549
549
# ' @export
550
- get_pred_type <- function (mod , type ) {
551
- check_mod_val(mod , existence = TRUE )
552
- pred_name <- paste0(mod , " _predict" )
550
+ get_pred_type <- function (model , type ) {
551
+ check_mod_val(model , existence = TRUE )
552
+ pred_name <- paste0(model , " _predict" )
553
553
if (! any(pred_name != rlang :: env_names(get_model_env()))) {
554
- stop(" `" , mod , " ` does not have any `pred` methods in parsnip." , call. = FALSE )
554
+ stop(" `" , model , " ` does not have any `pred` methods in parsnip." , call. = FALSE )
555
555
}
556
556
all_preds <- rlang :: env_get(get_model_env(), pred_name )
557
557
if (! any(all_preds $ type == type )) {
558
- stop(" `" , mod , " ` does not have any `" , type ,
558
+ stop(" `" , model , " ` does not have any `" , type ,
559
559
" ` prediction methods in parsnip." , call. = FALSE )
560
560
}
561
561
dplyr :: filter(all_preds , type == !! type )
@@ -564,7 +564,7 @@ get_pred_type <- function(mod, type) {
564
564
# ------------------------------------------------------------------------------
565
565
566
566
# ' @export
567
- validate_model <- function (mod ) {
567
+ validate_model <- function (model ) {
568
568
# check for consistency across engines, modes, args, etc
569
569
}
570
570
@@ -573,19 +573,19 @@ validate_model <- function(mod) {
573
573
# ' @rdname get_model_env
574
574
# ' @keywords internal
575
575
# ' @export
576
- show_model_info <- function (mod ) {
577
- check_mod_val(mod , existence = TRUE )
576
+ show_model_info <- function (model ) {
577
+ check_mod_val(model , existence = TRUE )
578
578
current <- get_model_env()
579
579
580
- cat(" Information for `" , mod , " `\n " , sep = " " )
580
+ cat(" Information for `" , model , " `\n " , sep = " " )
581
581
582
582
cat(
583
583
" modes:" ,
584
- paste0(current [[paste0(mod , " _modes" )]], collapse = " , " ),
584
+ paste0(current [[paste0(model , " _modes" )]], collapse = " , " ),
585
585
" \n "
586
586
)
587
587
588
- engines <- current [[paste0(mod )]]
588
+ engines <- current [[paste0(model )]]
589
589
if (nrow(engines ) > 0 ) {
590
590
cat(" engines: \n " )
591
591
engines %> %
@@ -606,7 +606,7 @@ show_model_info <- function(mod) {
606
606
cat(" no registered engines yet." )
607
607
}
608
608
609
- args <- current [[paste0(mod , " _args" )]]
609
+ args <- current [[paste0(model , " _args" )]]
610
610
if (nrow(args ) > 0 ) {
611
611
cat(" arguments: \n " )
612
612
args %> %
@@ -629,7 +629,7 @@ show_model_info <- function(mod) {
629
629
cat(" no registered arguments yet." )
630
630
}
631
631
632
- fits <- current [[paste0(mod , " _fits" )]]
632
+ fits <- current [[paste0(model , " _fits" )]]
633
633
if (nrow(fits ) > 0 ) {
634
634
635
635
} else {
0 commit comments