Use of the R matrix method for bound-state calculations. I. General theory