R/extract_vars.R

Defines functions extract_vars

Documented in extract_vars

#' @title Extract Variables from a Formula
#'
#' @description
#' Given a [formula()] `f`, returns all variables used on the left-hand side and
#' right-hand side of the formula.
#'
#' @param f (`formula()`).
#'
#' @return (`list()`) with elements `"lhs"` and `"rhs"`, both `character()`.
#' @export
#' @examples
#' extract_vars(Species ~ Sepal.Width + Sepal.Length)
#' extract_vars(Species ~ .)
extract_vars = function(f) {
  assert_formula(f)
  res = named_list(c("lhs", "rhs"))
  res$lhs = if (length(f) == 2L) character(0L) else all.vars(f[[2L]])
  res$rhs = all.vars(f[[length(f)]])
  res
}

Try the mlr3misc package in your browser

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

mlr3misc documentation built on Sept. 20, 2023, 5:06 p.m.