Skip to contents

Determine required packages for a model

Usage

# S3 method for class 'model_spec'
required_pkgs(x, infra = TRUE, ...)

# S3 method for class 'model_fit'
required_pkgs(x, infra = TRUE, ...)

Arguments

x

A model specification or fit.

infra

Should parsnip itself be included in the result?

...

Not used.

Value

A character vector

Examples

should_fail <- try(required_pkgs(linear_reg(engine = NULL)), silent = TRUE)
should_fail
#> [1] "Error in required_pkgs(linear_reg(engine = NULL)) : \n  \033[1m\033[22mPlease set an engine.\n"
#> attr(,"class")
#> [1] "try-error"
#> attr(,"condition")
#> <error/rlang_error>
#> Error in `required_pkgs()`:
#> ! Please set an engine.
#> ---
#> Backtrace:
#>      
#>   1. ├─base::tryCatch(...)
#>   2. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. ├─base (local) tryCatchOne(...)
#>   4. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(...)
#>  13.   └─pkgdown::build_site(...)
#>  14.     └─pkgdown:::build_site_local(...)
#>  15.       └─pkgdown::build_reference(...)
#>  16. ─pkgdown:::unwrap_purrr_error(...)
#>  17. └─base::withCallingHandlers(...)
#>  18.         └─purrr::map(...)
#>  19.           └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#>  20. ─purrr:::with_indexed_errors(...)
#>  21. └─base::withCallingHandlers(...)
#>  22. purrr:::call_with_cleanup(...)
#>  23.             └─pkgdown (local) .f(.x[[i]], ...)
#>  24. base::withCallingHandlers(...)
#>  25.               └─pkgdown:::data_reference_topic(...)
#>  26.                 └─pkgdown:::run_examples(...)
#>  27.                   └─pkgdown:::highlight_examples(code, topic, env = env)
#>  28.                     └─downlit::evaluate_and_highlight(...)
#>  29.                       └─evaluate::evaluate(code, child_env(env), new_device = TRUE, output_handler = output_handler)
#>  30. base::withRestarts(...)
#>  31. └─base (local) withRestartList(expr, restarts)
#>  32. ├─base (local) withOneRestart(withRestartList(expr, restarts[-nr]), restarts[[nr]])
#>  33. │ └─base (local) doWithOneRestart(return(expr), restart)
#>  34. └─base (local) withRestartList(expr, restarts[-nr])
#>  35. └─base (local) withOneRestart(expr, restarts[[1L]])
#>  36. └─base (local) doWithOneRestart(return(expr), restart)
#>  37. evaluate:::with_handlers(...)
#>  38. ├─base::eval(call)
#>  39. │ └─base::eval(call)
#>  40. └─base::withCallingHandlers(...)
#>  41. ─base::withVisible(eval(expr, envir))
#>  42.                         └─base::eval(expr, envir)
#>  43.                           └─base::eval(expr, envir)
#>  44. ─base::try(required_pkgs(linear_reg(engine = NULL)), silent = TRUE)
#>  45. └─base::tryCatch(...)
#>  46. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  47. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  48. └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  49. generics::required_pkgs(linear_reg(engine = NULL))
#>  50.                             └─parsnip:::required_pkgs.model_spec(linear_reg(engine = NULL))

linear_reg() |>
  set_engine("glmnet") |>
  required_pkgs()
#> [1] "parsnip" "glmnet" 

linear_reg() |>
  set_engine("glmnet") |>
  required_pkgs(infra = FALSE)
#> [1] "glmnet"

linear_reg() |>
  set_engine("lm") |>
  fit(mpg ~ ., data = mtcars) |>
  required_pkgs()
#> [1] "parsnip" "stats"