Density perturbation and cosmological evolution in the presence of magnetic field in $f(R)$ gravity models