In most supersymmetric models the stability of the proton is ensured by invoking R-parity. A necessary ingredient to enforce R-parity is the possibility of distinguishing the lepton superfields from the Higgs ones. This is generally achieved either by assuming different charges under some matter parity, or by assigning the superfields to different representations of a unified gauge group. We want to put forward the idea that the replica of the fermion generations, which constitute an intrinsic difference between the fermions and the Higgs superfields, can give a clue to understand R-parity as an accidental symmetry. More ambitiously, we suggest a possible relation between proton stability and the actual number of fermion generations. We carry out our investigation in the framework of non-Abelian horizontal gauge symmetries. We identify $SU(4)_H$ as the only acceptable horizontal gauge group whichcan naturally ensure the absence of R parity violating operators, without conflicting with other theoretical and phenomenological constraints. We analyze a version of the supersymmetric standard model equipped with a gauged horizontal $SU(4)_H$, in which R-parity is accidental. The model predicts four families of fermions, it allows for the dynamical generation of a realistic hierarchy of fermion masseswithout any ad hoc choice of small Yukawa couplings, it ensures in a natural way the heaviness of all the fourth family fermions (including the neutrino) and it predicts a {\it lower} limit for the $\tau$-neutrino mass of a few eV. The scale of the breaking of the horizontal symmetry can be constrained rather precisely in a narrow window around $\sim 10^{11}$ GeV. Some interesting astrophysical and cosmological implications of the model are addressed as well.