Passing column names dynamically to a recipe

I am trying to make a function that allows dynamic name passing to a recipe and since inline functions are not allowed in a recipe, I don't know how to go about it.

Reprex:

library(tidyverse)

data_tbl <- tibble(
  visit_date = seq(
    from = as.Date("2021-01-01"), 
    to   = as.Date("2021-10-15"),
    by = 7,
  ),
  visits = rnbinom(
    n = 42,
    size = 100,
    mu = 66
  )
)

ts_auto_recipe <- function(.data, .date_col, .pred_col){
  
  # * Tidyeval ----
  date_col_var <- rlang::enquo(.date_col)
  pred_col_var <- rlang::enquo(.pred_col)
  
  # * Checks ----
  if(!is.data.frame(.data)){
    stop(call. = FALSE, "You must supply a data.frame/tibble.")
  }
  
  if(rlang::quo_is_missing(date_col_var)){
    stop(call. = FALSE, "The (.date_col) must be supplied.")
  }
  
  if(rlang::quo_is_missing(pred_col_var)){
    stop(call. = FALSE, "The (.pred_col) must be supplied.")
  }
  
  # * Data ----
  data_tbl <- tibble::as_tibble(.data)
  
  data_tbl <- data_tbl %>%
    dplyr::select(
      {{ date_col_var }}, {{ pred_col_var }}, dplyr::everything()
    ) %>%
    dplyr::rename(
      date_col    = {{ date_col_var }}
      , value_col = {{ pred_col_var }}
    )
  
  # * Recipe Objects ----
  # ** Base recipe ----
  rec_base_obj <- recipes::recipe(
    formula = date_col ~ . # I have to do the above so I can do this, which I don't like
    , data = data_tbl
  )
  
  # * Add Steps ----
  # ** ts signature and normalize ----
  rec_date_obj <- rec_base_obj %>%
    timetk::step_timeseries_signature(date_col) %>%
    recipes::step_normalize(
      dplyr::contains("index.num")
      , dplyr::contains("date_col_year")
    )
  
  # * Recipe List ----
  rec_lst <- list(
    rec_base = rec_base_obj,
    rec_date = rec_date_obj
  )
  
  # * Return ----
  return(rec_lst)
  
}

I have also posted this on SO here

I feel I have answered my own question. Again the SO has the answer I made.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.