1

I have built custom recipes that I use in two of my packages. I suspect the issue may be the same across all. I will start with this one where the issue I am getting is at prep time as it is saying that the variable types are incorrect.

Here is the R file that creates the recipe step function:

step_hai_fourier <- function(recipe,
                             ...,
                             role = "predictor",
                             trained = FALSE,
                             columns = NULL,
                             scale_type = c("sin", "cos", "sincos"),
                             period = 1,
                             order = 1,
                             skip = FALSE,
                             id = rand_id("hai_fourier")) {
  terms <- recipes::ellipse_check(...)
  funcs <- c("sin", "cos", "sincos")
  if (!(scale_type %in% funcs)) {
    rlang::abort("`func` should be either `sin`, `cos`, or `sincos`")
  }

  recipes::add_step(
    recipe,
    step_hai_fourier_new(
      terms      = terms,
      role       = role,
      trained    = trained,
      columns    = columns,
      scale_type = scale_type,
      period     = period,
      order      = order,
      skip       = skip,
      id         = id
    )
  )
}

step_hai_fourier_new <-
  function(terms, role, trained, columns, scale_type, period, order, skip, id) {
    recipes::step(
      subclass   = "hai_fourier",
      terms      = terms,
      role       = role,
      trained    = trained,
      columns    = columns,
      scale_type = scale_type,
      period     = period,
      order      = order,
      skip       = skip,
      id         = id
    )
  }

#' @export
prep.step_hai_fourier <- function(x, training, info = NULL, ...) {
  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_fourier` must be `numeric`",
        "`integer` `double` classes."
      )
    )
  }

  step_hai_fourier_new(
    terms      = x$terms,
    role       = x$role,
    trained    = TRUE,
    columns    = col_names,
    scale_type = x$scale_type,
    period     = x$period,
    order      = x$order,
    skip       = x$skip,
    id         = x$id
  )
}

#' @export
bake.step_hai_fourier <- function(object, new_data, ...) {
  make_call <- function(col, period, order, scale_type) {
    rlang::call2(
      "hai_fourier_vec",
      .x = rlang::sym(col),
      .period = period,
      .order = order,
      .scale_type = scale_type,
      .ns = "healthyR.ai"
    )
  }

  grid <- expand.grid(
    col = object$columns,
    scale_type = object$scale_type,
    period = object$period,
    order = object$order,
    stringsAsFactors = FALSE
  )

  calls <- purrr::pmap(.l = list(grid$col, grid$period, grid$order, grid$scale_type), make_call)

  # Column Names
  newname <- paste0("fourier_", 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_fourier <-
  function(x, width = max(20, options()$width - 35), ...) {
    title <- "Fourier Transformation on "
    recipes::print_step(
      x$columns, x$terms, x$trained,
      width = width, title = title
    )
    invisible(x)
  }

#' Requited Packages
#' @rdname required_pkgs.healthyR.ai
#' @keywords internal
#' @return A character vector
#' @param x A recipe step
# @noRd
#' @export
required_pkgs.step_hai_fourier <- function(x, ...) {
  c("healthyR.ai")
}

Here is a simple example which is in my man page that is now failing:

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)
)

# Create a recipe object
rec_obj <- recipe(a ~ ., data = data_tbl) %>%
  step_hai_fourier(b, scale_type = "sin") %>%
  step_hai_fourier(b, scale_type = "cos") %>%
  step_hai_fourier(b, scale_type = "sincos")

# View the recipe object
>  rec_obj
Recipe

Inputs:

      role #variables
   outcome          1
 predictor          2

Operations:

Fourier Transformation on b
Fourier Transformation on b
Fourier Transformation on b

# Prepare the recipe object
>  prep(rec_obj)
Error in `prep()`:
! All variables for `step_hai_fourier` must be `numeric``integer` `double` classes.
Run `rlang::last_error()` to see where the error occurred.

# Bake the recipe object - Adds the Time Series Signature
bake(prep(rec_obj), data_tbl)

rec_obj %>% get_juiced_data()
 

Here is the trace:

> rlang::last_error()
<error/rlang_error>
Error in `prep()`:
! All variables for `step_hai_fourier` must be `numeric``integer` `double` classes.
---
Backtrace:
 1. recipes::prep(rec_obj)
 2. recipes:::prep.recipe(rec_obj)
 4. healthyR.ai:::prep.step_hai_fourier(x$steps[[i]], training = training, info = x$term_info)
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/rlang_error>
Error in `prep()`:
! All variables for `step_hai_fourier` must be `numeric``integer` `double` classes.
---
Backtrace:
    ▆
 1. ├─recipes::prep(rec_obj)
 2. └─recipes:::prep.recipe(rec_obj)
 3.   ├─recipes::prep(x$steps[[i]], training = training, info = x$term_info)
 4.   └─healthyR.ai:::prep.step_hai_fourier(x$steps[[i]], training = training, info = x$term_info)
 5.     └─rlang::abort(...)

Package requires recipes >= 1.0.0 and I have no clue what happened.

SESSION INFO

> sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] tidyquant_1.0.5            quantmod_0.4.20           
 [3] TTR_0.24.3                 PerformanceAnalytics_2.0.4
 [5] xts_0.12.2                 zoo_1.8-11                
 [7] lubridate_1.9.0            timechange_0.1.1          
 [9] timetk_2.8.1               healthyR.ai_0.0.9         
[11] yardstick_1.1.0            workflowsets_1.0.0        
[13] workflows_1.1.0            tune_1.0.1                
[15] rsample_1.1.0              recipes_1.0.3             
[17] parsnip_1.0.3              modeldata_1.0.1           
[19] infer_1.0.3                dials_1.1.0               
[21] scales_1.2.1               broom_1.0.1               
[23] tidymodels_1.0.0           forcats_0.5.2             
[25] stringr_1.4.1              dplyr_1.0.10              
[27] purrr_0.3.5                readr_2.1.3               
[29] tidyr_1.2.1                tibble_3.1.8              
[31] ggplot2_3.4.0              tidyverse_1.3.2           

loaded via a namespace (and not attached):
 [1] googledrive_2.0.0   colorspace_2.0-3    ellipsis_0.3.2      class_7.3-20       
 [5] fs_1.5.2            rstudioapi_0.14     listenv_0.8.0       furrr_0.3.1        
 [9] prodlim_2019.11.13  fansi_1.0.3         xml2_1.3.3          codetools_0.2-18   
[13] splines_4.2.1       knitr_1.40          jsonlite_1.8.3      dbplyr_2.2.1       
[17] compiler_4.2.1      httr_1.4.4          backports_1.4.1     assertthat_0.2.1   
[21] Matrix_1.5-3        gargle_1.2.1        cli_3.4.1           tools_4.2.1        
[25] gtable_0.3.1        glue_1.6.2          Rcpp_1.0.9          fracdiff_1.5-2     
[29] cellranger_1.1.0    DiceDesign_1.9      vctrs_0.5.0         nlme_3.1-160       
[33] urca_1.3-3          iterators_1.0.14    lmtest_0.9-40       timeDate_4021.106  
[37] gower_1.0.0         xfun_0.34           globals_0.16.1      rvest_1.0.3        
[41] lifecycle_1.0.3     googlesheets4_1.0.1 future_1.29.0       MASS_7.3-58.1      
[45] ipred_0.9-13        hms_1.1.2           parallel_4.2.1      curl_4.3.3         
[49] padr_0.6.1          rpart_4.1.19        stringi_1.7.8       highr_0.9          
[53] tseries_0.10-52     foreach_1.5.2       lhs_1.1.5           hardhat_1.2.0      
[57] lava_1.7.0          rlang_1.0.6         pkgconfig_2.0.3     evaluate_0.18      
[61] lattice_0.20-45     tidyselect_1.2.0    parallelly_1.32.1   magrittr_2.0.3     
[65] R6_2.5.1            generics_0.1.3      DBI_1.1.3           pillar_1.8.1       
[69] haven_2.5.1         withr_2.5.0         survival_3.4-0      nnet_7.3-18        
[73] future.apply_1.10.0 modelr_0.1.10       crayon_1.5.2        Quandl_2.11.0      
[77] utf8_1.2.2          tzdb_0.3.0          grid_4.2.1          readxl_1.4.1       
[81] forecast_8.18       reprex_2.0.2        digest_0.6.30       GPfit_1.0-8        
[85] munsell_0.5.0       sessioninfo_1.2.2   quadprog_1.5-8   

RStudio Version

RStudio 2022.07.2+576 "Spotted Wakerobin" Release (e7373ef832b49b2a9b88162cfe7eac5f22c40b34, 2022-09-06) for Windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82
  • well you have a date variable and you see this error right? `! All variables for \`step_hai_fourier\` must be \`numeric\`\`integer\` \`double\` classes` – rawr Nov 15 '22 at 19:09
  • but I'm selecting only the numeric column, this has always worked. I have not updated the code for prep since it was built. As seen in this link the example works: https://www.spsanderson.com/healthyR.ai/reference/step_hai_fourier.html?q=step_hai_fou#ref-examples – MCP_infiltrator Nov 15 '22 at 19:13
  • I seem to have fixed it with this change: `OLD: value_data <- info[info$variable %in% col_names, ]` `NEW: recipes::check_type(training[, col_names])` – MCP_infiltrator Nov 15 '22 at 19:41

1 Answers1

2

I needed to make the following change in the prep step

OLD

value_data <- info[info$variable %in% col_names, ]

NEW

recipes::check_type(training[, col_names])
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82
  • 1
    This change is related to the similar changes made in https://github.com/spsanderson/healthyR.ts/pull/368/files. I'm sorry for not sending the additional PR – EmilHvitfeldt Nov 15 '22 at 20:19
  • 1
    Don't be sorry, I got it figured out and even if I didn't figure it out, no need to be sorry. – MCP_infiltrator Nov 15 '22 at 20:46