added function to compute R automatically