1
#' Extract Predictor Names from Formula or Terms
2
#'
3
#' `all.vars` returns all variables used in a formula. This
4
#'  function only returns the variables explicitly used on the
5
#'  right-hand side (i.e., it will not resolve dots unless the
6
#'  object is terms with a data set specified).
7
#' @param object A model formula or [stats::terms()]
8
#'  object.
9
#' @param ... Arguments to pass to [all.vars()]
10
#' @return A character vector of names
11
#' @export
12
#' @examples
13
#' form_pred(y ~ x + z)
14
#' form_pred(terms(y ~ x + z))
15
#'
16
#' form_pred(y ~ x + log(z))
17
#' form_pred(log(y) ~ x + z)
18
#'
19
#' form_pred(y1 + y2 ~ x + z)
20
#' form_pred(log(y1) + y2 ~ x + z)
21
#'
22
#' # will fail:
23
#' # form_pred(y ~ .)
24
#'
25
#' form_pred(terms(mpg ~ (.)^2, data = mtcars))
26
#' form_pred(terms( ~ (.)^2, data = mtcars))
27
#' @importFrom stats terms
28

29
form_pred <- function(object, ...) {
30 1
  if(inherits(object, "formula")) {
31 1
    object <- terms(object)
32
  }
33 1
  y_index <- attr(object, "response")
34

35
  ## If there is something on the lhs of the formula,
36
  ## remove it and get vars
37 1
  if(y_index != 0) {
38 1
    object[[2]] <- NULL
39 1
    object <- terms(object)
40
  }
41 1
  all.vars(object, ...)
42
}

Read our documentation on viewing source code .

Loading