#' @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
}

