@@ -30,77 +30,24 @@ parsnip$modes <- c("regression", "classification", "unknown")
3030
3131# ------------------------------------------------------------------------------
3232
33- # ' @rdname get_model_env
34- # ' @keywords internal
35- # ' @export
3633pred_types <-
37- c(" raw" , " numeric" , " class" , " link " , " prob" , " conf_int" , " pred_int" , " quantile" )
34+ c(" raw" , " numeric" , " class" , " prob" , " conf_int" , " pred_int" , " quantile" )
3835
3936# ------------------------------------------------------------------------------
4037
41- # ' Tools to Register Models
38+ # ' Working with the parsnip model environment
4239# '
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.
4642# '
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.
8043# ' @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.
8647# ' @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.
9848# ' @references "Making a parsnip model from scratch"
9949# ' \url{https://tidymodels.github.io/parsnip/articles/articles/Scratch.html}
10050# ' @examples
101- # ' # Show the information about a model:
102- # ' show_model_info("rand_forest")
103- # '
10451# ' # Access the model data:
10552# ' current_code <- get_model_env()
10653# ' ls(envir = current_code)
@@ -109,10 +56,42 @@ pred_types <-
10956# ' @export
11057get_model_env <- function () {
11158 current <- utils :: getFromNamespace(" parsnip" , ns = " parsnip" )
112- # current <- parsnip
11359 current
11460}
11561
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+ # ------------------------------------------------------------------------------
11695
11796check_eng_val <- function (eng ) {
11897 if (rlang :: is_missing(eng ) || length(eng ) != 1 )
@@ -281,7 +260,68 @@ check_interface_val <- function(x) {
281260
282261# ------------------------------------------------------------------------------
283262
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")
285325# ' @keywords internal
286326# ' @export
287327set_new_model <- function (model ) {
@@ -320,7 +360,7 @@ set_new_model <- function(model) {
320360
321361# ------------------------------------------------------------------------------
322362
323- # ' @rdname get_model_env
363+ # ' @rdname set_new_model
324364# ' @keywords internal
325365# ' @export
326366set_model_mode <- function (model , mode ) {
@@ -340,7 +380,7 @@ set_model_mode <- function(model, mode) {
340380
341381# ------------------------------------------------------------------------------
342382
343- # ' @rdname get_model_env
383+ # ' @rdname set_new_model
344384# ' @keywords internal
345385# ' @export
346386set_model_engine <- function (model , mode , eng ) {
@@ -366,7 +406,7 @@ set_model_engine <- function(model, mode, eng) {
366406
367407# ------------------------------------------------------------------------------
368408
369- # ' @rdname get_model_env
409+ # ' @rdname set_new_model
370410# ' @keywords internal
371411# ' @export
372412set_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) {
412452
413453# ------------------------------------------------------------------------------
414454
415- # ' @rdname get_model_env
455+ # ' @rdname set_new_model
416456# ' @keywords internal
417457# ' @export
418458set_dependency <- function (model , eng , pkg ) {
@@ -456,7 +496,7 @@ set_dependency <- function(model, eng, pkg) {
456496 invisible (NULL )
457497}
458498
459- # ' @rdname get_model_env
499+ # ' @rdname set_new_model
460500# ' @keywords internal
461501# ' @export
462502get_dependency <- function (model ) {
@@ -471,7 +511,7 @@ get_dependency <- function(model) {
471511
472512# ------------------------------------------------------------------------------
473513
474- # ' @rdname get_model_env
514+ # ' @rdname set_new_model
475515# ' @keywords internal
476516# ' @export
477517set_fit <- function (model , mode , eng , value ) {
@@ -523,7 +563,7 @@ set_fit <- function(model, mode, eng, value) {
523563 invisible (NULL )
524564}
525565
526- # ' @rdname get_model_env
566+ # ' @rdname set_new_model
527567# ' @keywords internal
528568# ' @export
529569get_fit <- function (model ) {
@@ -537,7 +577,7 @@ get_fit <- function(model) {
537577
538578# ------------------------------------------------------------------------------
539579
540- # ' @rdname get_model_env
580+ # ' @rdname set_new_model
541581# ' @keywords internal
542582# ' @export
543583set_pred <- function (model , mode , eng , type , value ) {
@@ -590,7 +630,7 @@ set_pred <- function(model, mode, eng, type, value) {
590630 invisible (NULL )
591631}
592632
593- # ' @rdname get_model_env
633+ # ' @rdname set_new_model
594634# ' @keywords internal
595635# ' @export
596636get_pred_type <- function (model , type ) {
@@ -609,7 +649,7 @@ get_pred_type <- function(model, type) {
609649
610650# ------------------------------------------------------------------------------
611651
612- # ' @rdname get_model_env
652+ # ' @rdname set_new_model
613653# ' @keywords internal
614654# ' @export
615655show_model_info <- function (model ) {
@@ -700,35 +740,9 @@ show_model_info <- function(model) {
700740 invisible (NULL )
701741}
702742
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-
729743# ------------------------------------------------------------------------------
730744
731- # ' @rdname get_model_env
745+ # ' @rdname set_new_model
732746# ' @keywords internal
733747# ' @export
734748pred_value_template <- function (pre = NULL , post = NULL , func , ... ) {
0 commit comments