From fd10770fa261d872131a0e28c675c32cd49c77ed Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Sat, 7 Nov 2020 16:54:23 -0500 Subject: [PATCH 1/2] use generics pacakge and re-export --- DESCRIPTION | 2 +- NAMESPACE | 32 +++++++----- NEWS.md | 2 + R/0_imports.R | 102 ++++++------------------------------- man/reexports.Rd | 6 ++- man/required_pkgs.embed.Rd | 18 +++---- man/tunable.step_embed.Rd | 10 ++-- 7 files changed, 54 insertions(+), 118 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f52c9923..390d3533 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -36,7 +36,7 @@ Suggests: License: GPL-2 Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.1.9000 Roxygen: list(markdown = TRUE) ByteCompile: true URL: https://embed.tidymodels.org, https://github.com/tidymodels/embed diff --git a/NAMESPACE b/NAMESPACE index 857dd354..b5ddc373 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,15 @@ S3method(print,step_lencode_bayes) S3method(print,step_lencode_glm) S3method(print,step_lencode_mixed) S3method(print,step_umap) +S3method(required_pkgs,step_discretize_cart) +S3method(required_pkgs,step_discretize_xgb) +S3method(required_pkgs,step_embed) +S3method(required_pkgs,step_feature_hash) +S3method(required_pkgs,step_lencode_bayes) +S3method(required_pkgs,step_lencode_glm) +S3method(required_pkgs,step_lencode_mixed) +S3method(required_pkgs,step_umap) +S3method(required_pkgs,step_woe) S3method(tidy,step_discretize_cart) S3method(tidy,step_discretize_xgb) S3method(tidy,step_embed) @@ -34,19 +43,16 @@ S3method(tidy,step_lencode_glm) S3method(tidy,step_lencode_mixed) S3method(tidy,step_umap) S3method(tidy,step_woe) +S3method(tunable,step_discretize_cart) +S3method(tunable,step_discretize_xgb) +S3method(tunable,step_embed) +S3method(tunable,step_umap) +S3method(tunable,step_woe) export(add_woe) export(dictionary) export(embed_control) export(is_tf_available) -export(required_pkgs.step_discretize_cart) -export(required_pkgs.step_discretize_xgb) -export(required_pkgs.step_embed) -export(required_pkgs.step_feature_hash) -export(required_pkgs.step_lencode_bayes) -export(required_pkgs.step_lencode_glm) -export(required_pkgs.step_lencode_mixed) -export(required_pkgs.step_umap) -export(required_pkgs.step_woe) +export(required_pkgs) export(step_discretize_cart) export(step_discretize_xgb) export(step_embed) @@ -62,11 +68,7 @@ export(tidy.step_lencode_bayes) export(tidy.step_lencode_glm) export(tidy.step_lencode_mixed) export(tidy.step_umap) -export(tunable.step_discretize_cart) -export(tunable.step_discretize_xgb) -export(tunable.step_embed) -export(tunable.step_umap) -export(tunable.step_woe) +export(tunable) import(recipes) import(rlang) importFrom(dplyr,"%>%") @@ -80,7 +82,9 @@ importFrom(dplyr,left_join) importFrom(dplyr,mutate) importFrom(dplyr,one_of) importFrom(dplyr,tibble) +importFrom(generics,required_pkgs) importFrom(generics,tidy) +importFrom(generics,tunable) importFrom(keras,backend) importFrom(keras,compile) importFrom(keras,fit) diff --git a/NEWS.md b/NEWS.md index 969a22e2..000055b3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # embed (development version) + * More changes to enable better parallel processing on windows. + # embed 0.1.2 * Changes to enable better parallel processing on windows. diff --git a/R/0_imports.R b/R/0_imports.R index d8be2336..482c155f 100644 --- a/R/0_imports.R +++ b/R/0_imports.R @@ -17,6 +17,21 @@ #' @importFrom tidyr gather #' @importFrom withr with_seed + +# ------------------------------------------------------------------------------ + +#' @importFrom generics tidy +#' @export +generics::tidy + +#' @importFrom generics required_pkgs +#' @export +generics::required_pkgs + +#' @importFrom generics tunable +#' @export +generics::tunable + # ------------------------------------------------------------------------------ utils::globalVariables( @@ -30,90 +45,3 @@ utils::globalVariables( ) ) -# ------------------------------------------------------------------------------ - -# nocov start -.onLoad <- function(libname, pkgname) { - # This package has specific methods for the `tunable` generic. That generic - # is defined in the `tune` package. As of R 4.0, we need to register them. - embed_exports <- getNamespaceExports(ns = "embed") - names <- names(embed_exports) - tunable_steps <- grep("tunable.step", embed_exports, fixed = TRUE, - value = TRUE) - for (i in tunable_steps) { - s3_register("tune::tunable", i) - } - - # ---------------------------------------------------------------------------- - - if (rlang::is_installed("tune") && utils::packageVersion("tune") >= "0.1.1.9000") { - - req_pkgs_names <- grep("^required_pkgs\\.", names, value = TRUE) - req_pkgs_classes <- gsub("required_pkgs.", "", req_pkgs_names) - - for (i in seq_along(req_pkgs_names)) { - class <- req_pkgs_classes[[i]] - s3_register("tune::required_pkgs", class) - } - } -} - -s3_register <- function(generic, class, method = NULL) { - stopifnot(is.character(generic), length(generic) == 1) - stopifnot(is.character(class), length(class) == 1) - - pieces <- strsplit(generic, "::")[[1]] - stopifnot(length(pieces) == 2) - package <- pieces[[1]] - generic <- pieces[[2]] - - caller <- parent.frame() - - get_method_env <- function() { - top <- topenv(caller) - if (isNamespace(top)) { - asNamespace(environmentName(top)) - } else { - caller - } - } - get_method <- function(method, env) { - if (is.null(method)) { - get(paste0(generic, ".", class), envir = get_method_env()) - } else { - method - } - } - - method_fn <- get_method(method) - stopifnot(is.function(method_fn)) - - # Always register hook in case package is later unloaded & reloaded - setHook( - packageEvent(package, "onLoad"), - function(...) { - ns <- asNamespace(package) - - # Refresh the method, it might have been updated by `devtools::load_all()` - method_fn <- get_method(method) - - registerS3method(generic, class, method_fn, envir = ns) - } - ) - - # Avoid registration failures during loading (pkgload or regular) - if (!isNamespaceLoaded(package)) { - return(invisible()) - } - - envir <- asNamespace(package) - - # Only register if generic can be accessed - if (exists(generic, envir)) { - registerS3method(generic, class, method_fn, envir = envir) - } - - invisible() -} - -# nocov end diff --git a/man/reexports.Rd b/man/reexports.Rd index 05f9b436..e72a92fc 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -1,9 +1,11 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/reexports.R +% Please edit documentation in R/0_imports.R, R/reexports.R \docType{import} \name{reexports} \alias{reexports} \alias{tidy} +\alias{required_pkgs} +\alias{tunable} \title{Objects exported from other packages} \keyword{internal} \description{ @@ -11,6 +13,6 @@ These objects are imported from other packages. Follow the links below to see their documentation. \describe{ - \item{generics}{\code{\link[generics]{tidy}}} + \item{generics}{\code{\link[generics]{required_pkgs}}, \code{\link[generics]{tidy}}, \code{\link[generics]{tidy}}, \code{\link[generics]{tunable}}} }} diff --git a/man/required_pkgs.embed.Rd b/man/required_pkgs.embed.Rd index 5e2e3dcb..4be5ad4f 100644 --- a/man/required_pkgs.embed.Rd +++ b/man/required_pkgs.embed.Rd @@ -13,23 +13,23 @@ \alias{required_pkgs.step_woe} \title{S3 methods for tracking which additional packages are needed for steps.} \usage{ -required_pkgs.step_lencode_bayes(x, ...) +\method{required_pkgs}{step_lencode_bayes}(x, ...) -required_pkgs.step_discretize_cart(x, ...) +\method{required_pkgs}{step_discretize_cart}(x, ...) -required_pkgs.step_discretize_xgb(x, ...) +\method{required_pkgs}{step_discretize_xgb}(x, ...) -required_pkgs.step_lencode_glm(x, ...) +\method{required_pkgs}{step_lencode_glm}(x, ...) -required_pkgs.step_feature_hash(x, ...) +\method{required_pkgs}{step_feature_hash}(x, ...) -required_pkgs.step_lencode_mixed(x, ...) +\method{required_pkgs}{step_lencode_mixed}(x, ...) -required_pkgs.step_embed(x, ...) +\method{required_pkgs}{step_embed}(x, ...) -required_pkgs.step_umap(x, ...) +\method{required_pkgs}{step_umap}(x, ...) -required_pkgs.step_woe(x, ...) +\method{required_pkgs}{step_woe}(x, ...) } \arguments{ \item{x}{A recipe step} diff --git a/man/tunable.step_embed.Rd b/man/tunable.step_embed.Rd index 11a39813..163ed168 100644 --- a/man/tunable.step_embed.Rd +++ b/man/tunable.step_embed.Rd @@ -8,15 +8,15 @@ \alias{tunable.step_discretize_cart} \title{tunable methods for embed} \usage{ -tunable.step_embed(x, ...) +\method{tunable}{step_embed}(x, ...) -tunable.step_umap(x, ...) +\method{tunable}{step_umap}(x, ...) -tunable.step_woe(x, ...) +\method{tunable}{step_woe}(x, ...) -tunable.step_discretize_xgb(x, ...) +\method{tunable}{step_discretize_xgb}(x, ...) -tunable.step_discretize_cart(x, ...) +\method{tunable}{step_discretize_cart}(x, ...) } \arguments{ \item{x}{A recipe step object} From b5413ba123d10036867c3910929432ced6c9200a Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Sat, 7 Nov 2020 16:55:40 -0500 Subject: [PATCH 2/2] closes #57 --- NAMESPACE | 1 + R/woe.R | 1 + 2 files changed, 2 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index b5ddc373..12f00e7e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ S3method(print,step_lencode_bayes) S3method(print,step_lencode_glm) S3method(print,step_lencode_mixed) S3method(print,step_umap) +S3method(print,step_woe) S3method(required_pkgs,step_discretize_cart) S3method(required_pkgs,step_discretize_xgb) S3method(required_pkgs,step_embed) diff --git a/R/woe.R b/R/woe.R index 627f1e5a..20b19d87 100644 --- a/R/woe.R +++ b/R/woe.R @@ -407,6 +407,7 @@ bake.step_woe <- function(object, new_data, ...) { as_tibble(new_data) } +#' @export print.step_woe <- function(x, width = max(20, options()$width - 29), ...) { cat("WoE version against outcome", rlang::quo_text(x$outcome), "for ") printer(unique(x$dictionary$variable), x$terms, x$trained, width = width)