  We derive the conditions under which dark energy models whose Lagrangian
densities f are written in terms of the Ricci scalar R are cosmologically
viable. We show that the cosmological behavior of f(R) models can be understood
by a geometrical approach consisting in studying the m(r) curve on the (r, m)
plane, where m=Rf_{,RR}/f_{,R} and r=-Rf_{,R}/f with f_{,R}=df/dR. This allows
us to classify the f(R) models into four general classes, depending on the
existence of a standard matter epoch and on the final accelerated stage. The
existence of a viable matter dominated epoch prior to a late-time acceleration
requires that the variable m satisfies the conditions m(r) approx+0 and
dm/dr>-1 at r approx-1. For the existence of a viable late-time acceleration we
require instead either (i) m=-r-1, (sqrt{3}-1)/2<m<1 and dm/dr<-1 or (ii) 0<m<1
at r=-2. These conditions identify two regions in the (r, m) space, one for the
matter era and the other for the acceleration. Only models with a m(r) curve
that connects these regions and satisfy the requirements above lead to an
acceptable cosmology. The models of the type f(R)=alpha R^{-n} and f=R+alpha
R^{-n} do not satisfy these conditions for any n>0 and n<-1 and are thus
cosmologically unacceptable. Similar conclusions can be reached for many other
examples discussed in the text. In most cases the standard matter era is
replaced by a cosmic expansion with scale factor a propto t^{1/2}. We also find
that f(R) models can have a strongly phantom attractor but in this case there
is no acceptable matter era.
