title: How to reliably get dependent variable name from formula object?
body: <p>Let's say I have the following formula:</p>
  
  <pre><code>myformula&lt;-formula("depVar ~ Var1 + Var2")
  </code></pre>
  
  <p>How to reliably get dependent variable name from formula object? </p>
  
  <p>I failed to find any built-in function that serves this purpose.
  I know that <code>as.character(myformula)[[2]]</code> works, as do </p>
  
  <pre><code>sub("^(\\w*)\\s~\\s.*$","\\1",deparse(myform))
  </code></pre>
  
  <p>It just seems to me, that these methods are more a hackery, than a reliable and standard method to do it.</p>
  
  <hr>
  
  <p>Does anyone know perchance what exactly method the e.g. <code>lm</code> use? I've seen it's code, but it is a little to cryptic to me... here is a quote for your convenience:</p>
  
  <pre><code>    &gt; lm
  function (formula, data, subset, weights, na.action, method = "qr", 
      model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE, 
      contrasts = NULL, offset, ...) 
  {
      ret.x &lt;- x
      ret.y &lt;- y
      cl &lt;- match.call()
      mf &lt;- match.call(expand.dots = FALSE)
      m &lt;- match(c("formula", "data", "subset", "weights", "na.action", 
          "offset"), names(mf), 0L)
      mf &lt;- mf[c(1L, m)]
      mf$drop.unused.levels &lt;- TRUE
      mf[[1L]] &lt;- as.name("model.frame")
      mf &lt;- eval(mf, parent.frame())
      if (method == "model.frame") 
          return(mf)
      else if (method != "qr") 
          warning(gettextf("method = '%s' is not supported. Using 'qr'", 
              method), domain = NA)
      mt &lt;- attr(mf, "terms")
      y &lt;- model.response(mf, "numeric")
      w &lt;- as.vector(model.weights(mf))
      if (!is.null(w) &amp;&amp; !is.numeric(w)) 
          stop("'weights' must be a numeric vector")
      offset &lt;- as.vector(model.offset(mf))
      if (!is.null(offset)) {
          if (length(offset) != NROW(y)) 
              stop(gettextf("number of offsets is %d, should equal %d (number of observations)", 
                  length(offset), NROW(y)), domain = NA)
      }
      if (is.empty.model(mt)) {
          x &lt;- NULL
          z &lt;- list(coefficients = if (is.matrix(y)) matrix(, 0, 
              3) else numeric(), residuals = y, fitted.values = 0 * 
              y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w != 
              0) else if (is.matrix(y)) nrow(y) else length(y))
          if (!is.null(offset)) {
              z$fitted.values &lt;- offset
              z$residuals &lt;- y - offset
          }
      }
      else {
          x &lt;- model.matrix(mt, mf, contrasts)
          z &lt;- if (is.null(w)) 
              lm.fit(x, y, offset = offset, singular.ok = singular.ok, 
                  ...)
          else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok, 
              ...)
      }
      class(z) &lt;- c(if (is.matrix(y)) "mlm", "lm")
      z$na.action &lt;- attr(mf, "na.action")
      z$offset &lt;- offset
      z$contrasts &lt;- attr(x, "contrasts")
      z$xlevels &lt;- .getXlevels(mt, mf)
      z$call &lt;- cl
      z$terms &lt;- mt
      if (model) 
          z$model &lt;- mf
      if (ret.x) 
          z$x &lt;- x
      if (ret.y) 
          z$y &lt;- y
      if (!qr) 
          z$qr &lt;- NULL
      z
  }
  </code></pre>