Skip to content

Commit

Permalink
Merge pull request #60 from tidymodels/re-exports
Browse files Browse the repository at this point in the history
Use generics packages for S3 generics
  • Loading branch information
topepo committed Nov 7, 2020
2 parents 8dcddf4 + b5413ba commit 97f56bd
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 118 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 19 additions & 14 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ 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)
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)
Expand All @@ -34,19 +44,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)
Expand All @@ -62,11 +69,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,"%>%")
Expand All @@ -80,7 +83,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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
102 changes: 15 additions & 87 deletions R/0_imports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
1 change: 1 addition & 0 deletions R/woe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions man/reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions man/required_pkgs.embed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/tunable.step_embed.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 97f56bd

Please sign in to comment.