R/trans_pipeline.R

Defines functions transform.trans_pipeline fit.trans_pipeline trans_pipeline

Documented in trans_pipeline

#' @title Transform pipeline
#' @description A [daltoolbox::dal_transform()] that chains a sequence of transforms. `fit()` fits
#'  each step on the output of the previous one, and `transform()` applies the fitted steps in order.
#'  It lets a model take its preprocessing as a configurable list of transforms instead of a fixed
#'  one-hot-plus-normalization.
#' @param steps A list of `dal_transform` objects (unfitted prototypes), applied in order.
#' @return A `trans_pipeline` object.
#' @examples
#' library(daltoolbox)
#' pipe <- trans_pipeline(list(one_hot(), zscore()))
#' pipe <- fit(pipe, iris)
#' head(transform(pipe, iris))
#' @import daltoolbox
#' @export
trans_pipeline <- function(steps) {
  obj <- dal_transform()
  obj$steps <- steps
  obj$fitted <- NULL
  class(obj) <- append("trans_pipeline", class(obj))
  obj
}

#' @export
fit.trans_pipeline <- function(obj, data, ...) {
  fitted <- vector("list", length(obj$steps))
  current <- data
  for (i in seq_along(obj$steps)) {
    fitted[[i]] <- fit(obj$steps[[i]], current)
    current <- transform(fitted[[i]], current)
  }
  obj$fitted <- fitted
  obj
}

#' @export
transform.trans_pipeline <- function(obj, data, ...) {
  steps <- if (!is.null(obj$fitted)) obj$fitted else obj$steps
  current <- data
  for (step in steps) current <- transform(step, current)
  current
}

Try the heimdall package in your browser

Any scripts or data that you put into this service are public.

heimdall documentation built on Dec. 2, 2026, 5:08 p.m.