formula.procmod.frame.R 692 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#' @include internals.R
NULL

#' Build a formula from a procmod.frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
formula.procmod.frame = function (x, ...)
{
  nm <- sapply(names(x), as.name)

  if (length(nm) > 1L) {
    rhs <- nm[-1L]
    lhs <- nm[1L]
  }
  else if (length(nm) == 1L) {
    rhs <- nm[1L]
    lhs <- NULL
  }
  else stop("cannot create a formula from an empty procmod.frame")

  ff <- parse(text = paste(lhs,
                           paste(rhs,
                                 collapse = "+"),
                           sep = "~"),
              keep.source = FALSE)

  ff <- eval(ff)

  environment(ff) <- parent.frame()

  ff
}