Usage D(expr, name)
... Arguments expr a expression or call or (except D) a formula with no lhs. name,namevec character vector, giving the variable names (only one for D()) with respect to which derivatives will be computed.
Edit: I ignored part of the docs myself, before.
D won't accept a formula as expr. Try D(expression(x^2), "x").