factor out r environment detection