@@ -30,77 +30,24 @@ parsnip$modes <- c("regression", "classification", "unknown")
30
30
31
31
# ------------------------------------------------------------------------------
32
32
33
- # ' @rdname get_model_env
34
- # ' @keywords internal
35
- # ' @export
36
33
pred_types <-
37
- c(" raw" , " numeric" , " class" , " link " , " prob" , " conf_int" , " pred_int" , " quantile" )
34
+ c(" raw" , " numeric" , " class" , " prob" , " conf_int" , " pred_int" , " quantile" )
38
35
39
36
# ------------------------------------------------------------------------------
40
37
41
- # ' Tools to Register Models
38
+ # ' Working with the parsnip model environment
42
39
# '
43
- # ' These functions are similar to constructors and can be used to validate
44
- # ' that there are no conflicts with the underlying model structures used by the
45
- # ' package.
40
+ # ' These functions read and write to the environment where the package stores
41
+ # ' information about model specifications.
46
42
# '
47
- # ' @param model A single character string for the model type (e.g.
48
- # ' `"rand_forest"`, etc).
49
- # ' @param new A single logical to check to see if the model that you are check
50
- # ' has not already been registered.
51
- # ' @param existence A single logical to check to see if the model has already
52
- # ' been registered.
53
- # ' @param mode A single character string for the model mode (e.g. "regression").
54
- # ' @param eng A single character string for the model engine.
55
- # ' @param arg A single character string for the model argument name.
56
- # ' @param has_submodel A single logical for whether the argument
57
- # ' can make predictions on mutiple submodels at once.
58
- # ' @param func A named character vector that describes how to call
59
- # ' a function. `func` should have elements `pkg` and `fun`. The
60
- # ' former is optional but is recommended and the latter is
61
- # ' required. For example, `c(pkg = "stats", fun = "lm")` would be
62
- # ' used to invoke the usual linear regression function. In some
63
- # ' cases, it is helpful to use `c(fun = "predict")` when using a
64
- # ' package's `predict` method.
65
- # ' @param fit_obj A list with elements `interface`, `protect`,
66
- # ' `func` and `defaults`. See the package vignette "Making a
67
- # ' `parsnip` model from scratch".
68
- # ' @param pred_obj A list with elements `pre`, `post`, `func`, and `args`.
69
- # ' See the package vignette "Making a `parsnip` model from scratch".
70
- # ' @param type A single character value for the type of prediction. Possible
71
- # ' values are:
72
- # ' \Sexpr[results=rd]{paste0("'", parsnip::pred_types, "'", collapse = ", ")}.
73
- # ' @param pkg An options character string for a package name.
74
- # ' @param parsnip A single character string for the "harmonized" argument name
75
- # ' that `parsnip` exposes.
76
- # ' @param original A single character string for the argument name that
77
- # ' underlying model function uses.
78
- # ' @param value A list that conforms to the `fit_obj` or `pred_obj` description
79
- # ' above, depending on context.
80
43
# ' @param items A character string of objects in the model environment.
81
- # ' @param pre,post Optional functions for pre- and post-processing of prediction
82
- # ' results.
83
- # ' @param ... For `pred_value_template()`: optional arguments that should be
84
- # ' passed into the `args` slot for prediction objects. For `set_in_env()`,
85
- # ' named values that will be assigned to the model environment.
44
+ # ' @param ... Named values that will be assigned to the model environment.
45
+ # ' @param name A single character value for a new symbol in the model environment.
46
+ # ' @param value A single value for a new value in the model environment.
86
47
# ' @keywords internal
87
- # ' @details These functions are available for users to add their
88
- # ' own models or engines (in package or otherwise) so that they can
89
- # ' be accessed using `parsnip`. This are more thoroughly documented
90
- # ' on the package web site (see references below).
91
- # '
92
- # ' In short, `parsnip` stores an environment object that contains
93
- # ' all of the information and code about how models are used (e.g.
94
- # ' fitting, predicting, etc). These functions can be used to add
95
- # ' models to that environment as well as helper functions that can
96
- # ' be used to makes sure that the model data is in the right
97
- # ' format.
98
48
# ' @references "Making a parsnip model from scratch"
99
49
# ' \url{https://tidymodels.github.io/parsnip/articles/articles/Scratch.html}
100
50
# ' @examples
101
- # ' # Show the information about a model:
102
- # ' show_model_info("rand_forest")
103
- # '
104
51
# ' # Access the model data:
105
52
# ' current_code <- get_model_env()
106
53
# ' ls(envir = current_code)
@@ -109,10 +56,42 @@ pred_types <-
109
56
# ' @export
110
57
get_model_env <- function () {
111
58
current <- utils :: getFromNamespace(" parsnip" , ns = " parsnip" )
112
- # current <- parsnip
113
59
current
114
60
}
115
61
62
+ # ' @rdname get_model_env
63
+ # ' @keywords internal
64
+ # ' @export
65
+ get_from_env <- function (items ) {
66
+ mod_env <- get_model_env()
67
+ rlang :: env_get(mod_env , items )
68
+ }
69
+
70
+ # ' @rdname get_model_env
71
+ # ' @keywords internal
72
+ # ' @export
73
+ set_in_env <- function (... ) {
74
+ mod_env <- get_model_env()
75
+ rlang :: env_bind(mod_env , ... )
76
+ }
77
+
78
+ # ' @rdname get_model_env
79
+ # ' @keywords internal
80
+ # ' @export
81
+ set_env_val <- function (name , value ) {
82
+ if (length(name ) != 1 | length(value ) != 1 ) {
83
+ stop(" `name` and `value` should both be a single value." , call. = FALSE )
84
+ }
85
+ if (! is.character(name )) {
86
+ stop(" `name` should be a character value." , call. = FALSE )
87
+ }
88
+ mod_env <- get_model_env()
89
+ x <- list (value )
90
+ names(x ) <- name
91
+ rlang :: env_bind(mod_env , !!! x )
92
+ }
93
+
94
+ # ------------------------------------------------------------------------------
116
95
117
96
check_eng_val <- function (eng ) {
118
97
if (rlang :: is_missing(eng ) || length(eng ) != 1 )
@@ -281,7 +260,68 @@ check_interface_val <- function(x) {
281
260
282
261
# ------------------------------------------------------------------------------
283
262
284
- # ' @rdname get_model_env
263
+ # ' Tools to Register Models
264
+ # '
265
+ # ' These functions are similar to constructors and can be used to validate
266
+ # ' that there are no conflicts with the underlying model structures used by the
267
+ # ' package.
268
+ # '
269
+ # ' @param model A single character string for the model type (e.g.
270
+ # ' `"rand_forest"`, etc).
271
+ # ' @param new A single logical to check to see if the model that you are check
272
+ # ' has not already been registered.
273
+ # ' @param existence A single logical to check to see if the model has already
274
+ # ' been registered.
275
+ # ' @param mode A single character string for the model mode (e.g. "regression").
276
+ # ' @param eng A single character string for the model engine.
277
+ # ' @param arg A single character string for the model argument name.
278
+ # ' @param has_submodel A single logical for whether the argument
279
+ # ' can make predictions on mutiple submodels at once.
280
+ # ' @param func A named character vector that describes how to call
281
+ # ' a function. `func` should have elements `pkg` and `fun`. The
282
+ # ' former is optional but is recommended and the latter is
283
+ # ' required. For example, `c(pkg = "stats", fun = "lm")` would be
284
+ # ' used to invoke the usual linear regression function. In some
285
+ # ' cases, it is helpful to use `c(fun = "predict")` when using a
286
+ # ' package's `predict` method.
287
+ # ' @param fit_obj A list with elements `interface`, `protect`,
288
+ # ' `func` and `defaults`. See the package vignette "Making a
289
+ # ' `parsnip` model from scratch".
290
+ # ' @param pred_obj A list with elements `pre`, `post`, `func`, and `args`.
291
+ # ' See the package vignette "Making a `parsnip` model from scratch".
292
+ # ' @param type A single character value for the type of prediction. Possible
293
+ # ' values are: `class`, `conf_int`, `numeric`, `pred_int`, `prob`, `quantile`,
294
+ # ' and `raw`.
295
+ # ' @param pkg An options character string for a package name.
296
+ # ' @param parsnip A single character string for the "harmonized" argument name
297
+ # ' that `parsnip` exposes.
298
+ # ' @param original A single character string for the argument name that
299
+ # ' underlying model function uses.
300
+ # ' @param value A list that conforms to the `fit_obj` or `pred_obj` description
301
+ # ' above, depending on context.
302
+ # ' @param pre,post Optional functions for pre- and post-processing of prediction
303
+ # ' results.
304
+ # ' @param ... Optional arguments that should be passed into the `args` slot for
305
+ # ' prediction objects.
306
+ # ' @keywords internal
307
+ # ' @details These functions are available for users to add their
308
+ # ' own models or engines (in package or otherwise) so that they can
309
+ # ' be accessed using `parsnip`. This are more thoroughly documented
310
+ # ' on the package web site (see references below).
311
+ # '
312
+ # ' In short, `parsnip` stores an environment object that contains
313
+ # ' all of the information and code about how models are used (e.g.
314
+ # ' fitting, predicting, etc). These functions can be used to add
315
+ # ' models to that environment as well as helper functions that can
316
+ # ' be used to makes sure that the model data is in the right
317
+ # ' format.
318
+ # ' @references "Making a parsnip model from scratch"
319
+ # ' \url{https://tidymodels.github.io/parsnip/articles/articles/Scratch.html}
320
+ # ' @examples
321
+ # ' # set_new_model("shallow_learning_model")
322
+ # '
323
+ # ' # Show the information about a model:
324
+ # ' show_model_info("rand_forest")
285
325
# ' @keywords internal
286
326
# ' @export
287
327
set_new_model <- function (model ) {
@@ -320,7 +360,7 @@ set_new_model <- function(model) {
320
360
321
361
# ------------------------------------------------------------------------------
322
362
323
- # ' @rdname get_model_env
363
+ # ' @rdname set_new_model
324
364
# ' @keywords internal
325
365
# ' @export
326
366
set_model_mode <- function (model , mode ) {
@@ -340,7 +380,7 @@ set_model_mode <- function(model, mode) {
340
380
341
381
# ------------------------------------------------------------------------------
342
382
343
- # ' @rdname get_model_env
383
+ # ' @rdname set_new_model
344
384
# ' @keywords internal
345
385
# ' @export
346
386
set_model_engine <- function (model , mode , eng ) {
@@ -366,7 +406,7 @@ set_model_engine <- function(model, mode, eng) {
366
406
367
407
# ------------------------------------------------------------------------------
368
408
369
- # ' @rdname get_model_env
409
+ # ' @rdname set_new_model
370
410
# ' @keywords internal
371
411
# ' @export
372
412
set_model_arg <- function (model , eng , parsnip , original , func , has_submodel ) {
@@ -412,7 +452,7 @@ set_model_arg <- function(model, eng, parsnip, original, func, has_submodel) {
412
452
413
453
# ------------------------------------------------------------------------------
414
454
415
- # ' @rdname get_model_env
455
+ # ' @rdname set_new_model
416
456
# ' @keywords internal
417
457
# ' @export
418
458
set_dependency <- function (model , eng , pkg ) {
@@ -456,7 +496,7 @@ set_dependency <- function(model, eng, pkg) {
456
496
invisible (NULL )
457
497
}
458
498
459
- # ' @rdname get_model_env
499
+ # ' @rdname set_new_model
460
500
# ' @keywords internal
461
501
# ' @export
462
502
get_dependency <- function (model ) {
@@ -471,7 +511,7 @@ get_dependency <- function(model) {
471
511
472
512
# ------------------------------------------------------------------------------
473
513
474
- # ' @rdname get_model_env
514
+ # ' @rdname set_new_model
475
515
# ' @keywords internal
476
516
# ' @export
477
517
set_fit <- function (model , mode , eng , value ) {
@@ -523,7 +563,7 @@ set_fit <- function(model, mode, eng, value) {
523
563
invisible (NULL )
524
564
}
525
565
526
- # ' @rdname get_model_env
566
+ # ' @rdname set_new_model
527
567
# ' @keywords internal
528
568
# ' @export
529
569
get_fit <- function (model ) {
@@ -537,7 +577,7 @@ get_fit <- function(model) {
537
577
538
578
# ------------------------------------------------------------------------------
539
579
540
- # ' @rdname get_model_env
580
+ # ' @rdname set_new_model
541
581
# ' @keywords internal
542
582
# ' @export
543
583
set_pred <- function (model , mode , eng , type , value ) {
@@ -590,7 +630,7 @@ set_pred <- function(model, mode, eng, type, value) {
590
630
invisible (NULL )
591
631
}
592
632
593
- # ' @rdname get_model_env
633
+ # ' @rdname set_new_model
594
634
# ' @keywords internal
595
635
# ' @export
596
636
get_pred_type <- function (model , type ) {
@@ -609,7 +649,7 @@ get_pred_type <- function(model, type) {
609
649
610
650
# ------------------------------------------------------------------------------
611
651
612
- # ' @rdname get_model_env
652
+ # ' @rdname set_new_model
613
653
# ' @keywords internal
614
654
# ' @export
615
655
show_model_info <- function (model ) {
@@ -700,35 +740,9 @@ show_model_info <- function(model) {
700
740
invisible (NULL )
701
741
}
702
742
703
- # ' @rdname get_model_env
704
- # ' @keywords internal
705
- # ' @export
706
- get_from_env <- function (items ) {
707
- mod_env <- get_model_env()
708
- rlang :: env_get(mod_env , items )
709
- }
710
-
711
- # ' @rdname get_model_env
712
- # ' @keywords internal
713
- # ' @export
714
- set_in_env <- function (... ) {
715
- mod_env <- get_model_env()
716
- rlang :: env_bind(mod_env , ... )
717
- }
718
-
719
- # ' @rdname get_model_env
720
- # ' @keywords internal
721
- # ' @export
722
- set_env_val <- function (name , value ) {
723
- mod_env <- parsnip :: get_model_env()
724
- x <- list (value )
725
- names(x ) <- name
726
- rlang :: env_bind(mod_env , !!! x )
727
- }
728
-
729
743
# ------------------------------------------------------------------------------
730
744
731
- # ' @rdname get_model_env
745
+ # ' @rdname set_new_model
732
746
# ' @keywords internal
733
747
# ' @export
734
748
pred_value_template <- function (pre = NULL , post = NULL , func , ... ) {
0 commit comments