R/get_lhs.R

Defines functions get_lhs

Documented in get_lhs

#' @title Get left hand side of a formula
#' @description Returns the dependent variable in a formula given by a string or a \code{formula}
#' @param form Either a string in the form \code{'y ~ ...'} or an object of \code{formula} class
#' @return  A string with the name of the left hand side variable in the formula
#'
#' @importFrom stats as.formula
#' @importFrom methods is
#' 
get_lhs <- function(form) {
  if(is(form, "formula")){
    form <- paste(deparse(form), collapse = " ")
    form <- gsub("\\s+", " ", form, perl = FALSE)
  }
  if(is.character(form)){
    lhs_var <- unlist(strsplit(as.character(form), "~"))[1]
    lhs_var <- trimws(lhs_var)
    form <- as.formula(form)
  }

  return(lhs_var)
}

Try the nlstac package in your browser

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

nlstac documentation built on April 11, 2023, 6:12 p.m.