title: Symbolic derivatives on formulas
body: <p>In R, I would like a way to take symbolic derivatives of the right hand side of formulas which may include interaction terms, squared terms, etc.</p>
  
  <p>For example, I would like to be able to take the derivative of the right hand side of each of the following two [edit:three] formulas with respect to x:</p>
  
  <p>y~x+I(x^2)</p>
  
  <p>y~x:z</p>
  
  <p>EDIT: y~x*z</p>
  
  <p>I would like a function which, when each of the above three formulas are input, returns 1+2x, z, and 1+z, respectively.</p>
  
  <p>I've tried the following:</p>
  
  <pre><code>f1&lt;-y~x+I(x^2)
  deriv(f1,"x")
  ## Error in deriv.formula(f1, "x") : Function 'I' is not in the derivatives table
  
  f2&lt;-y~x:z
  deriv(f2,"x")
  ## Error in deriv.formula(f2, "x") : Function '`:`' is not in the derivatives table
  </code></pre>
  
  <p>Is there any way to force R to recognize I(x^2) (or, similarly, I(x*z), etc.) as x^2 (respectively, x*z), x:z as x*z (in the mathematical sense), and x*z (in the formula sense) as x+z+x*z (in the mathematical sense) for purposes of calculating the derivative?</p>
  
  <p>Second, is there a way to take the output from deriv() and reshape it to look like the right hand side of a formula? In particular, I know that D() will alleviate this issue and generate output in the form I desire (though D() can't handle a formula as input), but what if I want to take derivatives with respect to multiple variables? I can work around this by applying D() over and over for each variable I'd like to take the derivative with respect to, but it would be nice to simply input a character string of all such variables and receive output suitable to be placed on the right hand side of a formula.</p>
  
  <p>Thank you!</p>