From 8f278ef708c4d520d14bc83e662807e09bd0332b Mon Sep 17 00:00:00 2001 From: topepo Date: Mon, 25 Feb 2019 12:26:13 -0500 Subject: [PATCH 1/2] S3 control method for #128 --- R/fit_control.R | 22 +++++++++++++++++----- man/fit_control.Rd | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/R/fit_control.R b/R/fit_control.R index cfd66a4c3..32ea607ed 100644 --- a/R/fit_control.R +++ b/R/fit_control.R @@ -1,8 +1,8 @@ #' Control the fit function -#' -#' Options can be passed to the [fit()] function that control the output and +#' +#' Options can be passed to the [fit()] function that control the output and #' computations -#' +#' #' @param verbosity An integer where a value of zero indicates #' that no messages or output should be shown when packages are #' loaded or when the model is fit. A value of 1 means that package @@ -14,12 +14,24 @@ #' the model inside of `try(, silent = TRUE)`. If the model fails, #' an object is still returned (without an error) that inherits the #' class "try-error". -#' @return A named list with the results of the function call +#' @return An S3 object with class "fit_control" that is a named list with the +#' results of the function call #' @export -#' +#' fit_control <- function(verbosity = 1L, catch = FALSE) { res <- list(verbosity = verbosity, catch = catch) res <- check_control(res) + class(res) <- "fit_control" res } + +# '@export +print.fit_control <- function(x, ...) { + cat("parsnip control object\n") + if (x$verbosity > 1) + cat(" verbose mode\n") + if (x$catch > 1) + cat(" fit errors will be caught\n") + invisible(x) +} diff --git a/man/fit_control.Rd b/man/fit_control.Rd index c0c266e05..9a6cd08df 100644 --- a/man/fit_control.Rd +++ b/man/fit_control.Rd @@ -21,7 +21,8 @@ an object is still returned (without an error) that inherits the class "try-error".} } \value{ -A named list with the results of the function call +An S3 object with class "fit_control" that is a named list with the +results of the function call } \description{ Options can be passed to the \code{\link[=fit]{fit()}} function that control the output and From 6b79d98e835ae31fc872a17c69006c649699813b Mon Sep 17 00:00:00 2001 From: topepo Date: Mon, 25 Feb 2019 13:11:58 -0500 Subject: [PATCH 2/2] fixed control object printing --- NAMESPACE | 1 + NEWS.md | 2 ++ R/fit_control.R | 8 ++++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 662cfaff0..70f252d81 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ S3method(predict_raw,"_multnet") S3method(predict_raw,model_fit) S3method(print,boost_tree) S3method(print,decision_tree) +S3method(print,fit_control) S3method(print,linear_reg) S3method(print,logistic_reg) S3method(print,mars) diff --git a/NEWS.md b/NEWS.md index 0f76cee0c..775ee3b28 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,8 @@ of possible varying arguments is returned (as opposed to only the arguments that are actually varying). +* `fit_control()` not returns an S3 method. + ## Bug Fixes * `varying_args()` now uses the version from the `generics` package. This means diff --git a/R/fit_control.R b/R/fit_control.R index 32ea607ed..98742aa80 100644 --- a/R/fit_control.R +++ b/R/fit_control.R @@ -26,12 +26,12 @@ fit_control <- function(verbosity = 1L, catch = FALSE) { res } -# '@export +#' @export print.fit_control <- function(x, ...) { cat("parsnip control object\n") if (x$verbosity > 1) - cat(" verbose mode\n") - if (x$catch > 1) - cat(" fit errors will be caught\n") + cat(" - verbose level", x$verbosity, "\n") + if (x$catch) + cat(" - fit errors will be caught\n") invisible(x) }