Factoring some R integration code