Improve and test the error handling of the #R macro