r语言 - 没有适用于类对象的'prep'方法



我正在构建一个自定义的recipes函数,在尝试prep()配方时出错。我得到以下错误:

> prep(rec_obj)
Error in UseMethod("prep") : 
no applicable method for 'prep' applied to an object of class "c('step_hai_hyperbolic', 'step')"

当我将prep.step_hai_hyperbolic()作为一个函数加载到全局环境中,使其显示在RStudio的环境区域中时,prep()就可以正常工作。

以下是整个功能和一个示例:

step_hai_hyperbolic <- function(recipe,
...,
role       = "predictor",
trained    = FALSE,
columns    = NULL,
scale_type = c("sin","cos","tan"),
skip       = FALSE,
id         = rand_id("hai_hyperbolic")
){
terms <- recipes::ellipse_check(...)
funcs <- c("sin", "cos", "tan")
if (!(scale_type %in% funcs))
rlang::abort("`func` should be either `sin`, `cos`, or `tan`")
recipes::add_step(
recipe,
step_hai_hyperbolic_new(
terms      = terms,
role       = role,
trained    = trained,
columns    = columns,
scale_type = scale_type,
skip       = skip,
id         = id
)
)
}
step_hai_hyperbolic_new <-
function(terms, role, trained, columns, scale_type, skip, id){
recipes::step(
subclass   = "hai_hyperbolic",
terms      = terms,
role       = role,
trained    = trained,
columns    = columns,
scale_type = scale_type,
skip       = skip,
id         = id
)
}
#' @export
prep.step_hai_hyperbolic <- function(x, training, info = NULL, ...) {
#col_names <- recipes::recipes_eval_select(x$terms, training, info = info)
col_names <- recipes::recipes_eval_select(x$terms, training, info)
value_data <- info[info$variable %in% col_names, ]
if(any(value_data$type != "numeric")){
rlang::abort(
paste0("All variables for `step_hai_hyperbolic` must be `numeric`",
"`integer` `double` classes.")
)
}
step_hai_hyperbolic_new(
terms      = x$terms,
role       = x$role,
trained    = TRUE,
columns    = col_names,
scale_type = x$scale_type,
skip       = x$skip,
id         = x$id
)
}
#' @export
bake.step_hai_hyperbolic <- function(object, new_data, ...){
make_call <- function(col, scale_type){
rlang::call2(
"hai_hyperbolic_vec",
x              = rlang::sym(col)
,  .scale_type = scale_type
, .ns          = "healthyR.ai"
)
}
grid <- expand.grid(
col                = object$columns
, scale_type       = object$scale_type
, stringsAsFactors = FALSE
)
calls <- purrr::pmap(.l = list(grid$col, grid$scale_type), make_call)
# Column Names
newname <- paste0(grid$col, "_", grid$scale_type)
calls   <- recipes::check_name(calls, new_data, object, newname, TRUE)
tibble::as_tibble(dplyr::mutate(new_data, !!!calls))
}
#' @export
print.step_hai_hyperbolic <-
function(x, width = max(20, options()$width - 35), ...) {
cat("Hyperbolic transformation on ", sep = "")
printer(
# Names before prep (could be selectors)
untr_obj = x$terms,
# Names after prep:
tr_obj = names(x$columns),
# Has it been prepped?
trained = x$trained,
# An estimate of how many characters to print on a line:
width = width
)
invisible(x)
}
#' @rdname required_pkgs.healthyR.ai
#' @export
required_pkgs.step_hai_hyperbolic <- function(x, ...) {
c("healthyR.ai")
}

以下是一个在没有将所有内容加载到当前会话环境中的情况下无法工作的示例:

library(tidyverse)
library(tidymodels)
library(healthyR.ai)
len_out    = 10
by_unit    = "month"
start_date = as.Date("2021-01-01")
data_tbl <- tibble(
date_col = seq.Date(from = start_date, length.out = len_out, by = by_unit),
a    = rnorm(len_out),
b    = runif(len_out)
)
rec_obj <- recipe(a ~., data = data_tbl) %>%
step_hai_hyperbolic(b, scale_type = "sin") %>%
step_hai_hyperbolic(b, scale_type = "cos")

rec_obj到控制台的输出:

> rec_obj
Recipe
Inputs:
role #variables
outcome          1
predictor          2
Operations:
Hyperbolic transformation on b
Hyperbolic transformation on b

准备时的错误:

> prep(rec_obj)
Error in UseMethod("prep") : 
no applicable method for 'prep' applied to an object of class "c('step_hai_hyperbolic', 'step')"

会话信息:

> sessioninfo::session_info()
- Session info  ----------------------------------------------------------------------------------
hash: credit card, keyboard, flag: South Africa
setting  value
version  R version 4.1.0 (2021-05-18)
os       Windows 10 x64 (build 19042)
system   x86_64, mingw32
ui       RStudio
language (EN)
collate  English_United States.1252
ctype    English_United States.1252
tz       America/New_York
date     2021-11-05
rstudio  1.4.1717 Juliet Rose (desktop)
pandoc   NA
- Packages ---------------------------------------------------------------------------------------
package      * version    date (UTC) lib source
assertthat     0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
backports      1.3.0      2021-10-27 [1] CRAN (R 4.1.1)
broom        * 0.7.10     2021-10-31 [1] CRAN (R 4.1.1)
cellranger     1.1.0      2016-07-27 [1] CRAN (R 4.1.0)
class          7.3-19     2021-05-03 [2] CRAN (R 4.1.0)
cli            3.1.0      2021-10-27 [1] CRAN (R 4.1.0)
codetools      0.2-18     2020-11-04 [2] CRAN (R 4.1.0)
colorspace     2.0-2      2021-06-24 [1] CRAN (R 4.1.0)
crayon         1.4.2      2021-10-29 [1] CRAN (R 4.1.1)
DBI            1.1.1      2021-01-15 [1] CRAN (R 4.1.0)
dbplyr         2.1.1      2021-04-06 [1] CRAN (R 4.1.0)
dials        * 0.0.10     2021-09-10 [1] CRAN (R 4.1.0)
DiceDesign     1.9        2021-02-13 [1] CRAN (R 4.1.0)
digest         0.6.28     2021-09-23 [1] CRAN (R 4.1.1)
dplyr        * 1.0.7      2021-06-18 [1] CRAN (R 4.1.0)
ellipsis       0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
fansi          0.5.0      2021-05-25 [1] CRAN (R 4.1.0)
forcats      * 0.5.1      2021-01-27 [1] CRAN (R 4.1.0)
foreach        1.5.1      2020-10-15 [1] CRAN (R 4.1.0)
fs             1.5.0      2020-07-31 [1] CRAN (R 4.1.0)
furrr          0.2.3      2021-06-25 [1] CRAN (R 4.1.0)
future         1.23.0     2021-10-31 [1] CRAN (R 4.1.1)
future.apply   1.8.1      2021-08-10 [1] CRAN (R 4.1.0)
generics       0.1.1      2021-10-25 [1] CRAN (R 4.1.0)
ggplot2      * 3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
globals        0.14.0     2020-11-22 [1] CRAN (R 4.1.0)
glue           1.4.2      2020-08-27 [1] CRAN (R 4.1.0)
gower          0.2.2      2020-06-23 [1] CRAN (R 4.1.0)
GPfit          1.0-8      2019-02-08 [1] CRAN (R 4.1.0)
gtable         0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
hardhat        0.1.6      2021-07-14 [1] CRAN (R 4.1.0)
haven          2.4.3      2021-08-04 [1] CRAN (R 4.1.0)
healthyR.ai  * 0.0.2.9000 2021-11-05 [1] local
hms            1.1.1      2021-09-26 [1] CRAN (R 4.1.0)
httr           1.4.2      2020-07-20 [1] CRAN (R 4.1.0)
infer        * 1.0.0      2021-08-13 [1] CRAN (R 4.1.0)
ipred          0.9-12     2021-09-15 [1] CRAN (R 4.1.0)
iterators      1.0.13     2020-10-15 [1] CRAN (R 4.1.0)
jsonlite       1.7.2      2020-12-09 [1] CRAN (R 4.1.0)
lattice        0.20-44    2021-05-02 [2] CRAN (R 4.1.0)
lava           1.6.10     2021-09-02 [1] CRAN (R 4.1.0)
lhs            1.1.3      2021-09-08 [1] CRAN (R 4.1.0)
lifecycle      1.0.1      2021-09-24 [1] CRAN (R 4.1.0)
listenv        0.8.0      2019-12-05 [1] CRAN (R 4.1.0)
lubridate      1.8.0      2021-10-07 [1] CRAN (R 4.1.1)
magrittr       2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
MASS           7.3-54     2021-05-03 [2] CRAN (R 4.1.0)
Matrix         1.3-4      2021-06-01 [1] CRAN (R 4.1.0)
modeldata    * 0.1.1      2021-07-14 [1] CRAN (R 4.1.0)
modelr         0.1.8      2020-05-19 [1] CRAN (R 4.1.0)
munsell        0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
nnet           7.3-16     2021-05-03 [2] CRAN (R 4.1.0)
parallelly     1.28.1     2021-09-09 [1] CRAN (R 4.1.1)
parsnip      * 0.1.7      2021-07-21 [1] CRAN (R 4.1.0)
pillar         1.6.4      2021-10-18 [1] CRAN (R 4.1.0)
pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
plyr           1.8.6      2020-03-03 [1] CRAN (R 4.1.0)
pROC           1.18.0     2021-09-03 [1] CRAN (R 4.1.1)
prodlim        2019.11.13 2019-11-17 [1] CRAN (R 4.1.0)
purrr        * 0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
R6             2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
Rcpp           1.0.7      2021-07-07 [1] CRAN (R 4.1.0)
readr        * 2.0.2      2021-09-27 [1] CRAN (R 4.1.0)
readxl         1.3.1      2019-03-13 [1] CRAN (R 4.1.0)
recipes      * 0.1.17     2021-09-27 [1] CRAN (R 4.1.0)
reprex         2.0.1      2021-08-05 [1] CRAN (R 4.1.0)
rlang          0.4.12     2021-10-18 [1] CRAN (R 4.1.0)
rpart          4.1-15     2019-04-12 [2] CRAN (R 4.1.0)
rsample      * 0.1.0      2021-05-08 [1] CRAN (R 4.1.0)
rstudioapi     0.13       2020-11-12 [1] CRAN (R 4.1.0)
rvest          1.0.2      2021-10-16 [1] CRAN (R 4.1.0)
scales       * 1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
sessioninfo    1.2.1      2021-11-02 [1] CRAN (R 4.1.0)
stringi        1.7.5      2021-10-04 [1] CRAN (R 4.1.0)
stringr      * 1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
survival       3.2-11     2021-04-26 [2] CRAN (R 4.1.0)
tibble       * 3.1.5      2021-09-30 [1] CRAN (R 4.1.0)
tidymodels   * 0.1.4      2021-10-01 [1] CRAN (R 4.1.0)
tidyr        * 1.1.4      2021-09-27 [1] CRAN (R 4.1.0)
tidyselect     1.1.1      2021-04-30 [1] CRAN (R 4.1.0)
tidyverse    * 1.3.1      2021-04-15 [1] CRAN (R 4.1.0)
timeDate       3043.102   2018-02-21 [1] CRAN (R 4.1.0)
tune         * 0.1.6      2021-07-21 [1] CRAN (R 4.1.0)
tzdb           0.2.0      2021-10-27 [1] CRAN (R 4.1.0)
utf8           1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
vctrs          0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
withr          2.4.2      2021-04-18 [1] CRAN (R 4.1.0)
workflows    * 0.2.4      2021-10-12 [1] CRAN (R 4.1.1)
workflowsets * 0.1.0      2021-07-22 [1] CRAN (R 4.1.0)
xml2           1.3.2      2020-04-23 [1] CRAN (R 4.1.0)
yardstick    * 0.0.8      2021-03-28 [1] CRAN (R 4.1.0)
[1] C:/Users/Steve/Documents/R/win-library/4.1
[2] C:/Program Files/R/R-4.1.0/library
--------------------------------------------------------------------------------------------------

@importFrom recipes prep bake必须添加到.R文件

相关内容

  • 没有找到相关文章

最新更新