gravity from a variational principle

in classical mechanics and electrodynamics one obtains a second-order equation of motion from squares of the first derivatives, either of the position with time or of the derivatives of the potentials. gravity seems to be different: the Ricci-scalar in the Einstein-Hilbert action contains already second derivatives: can you explain this difference?