How to handle R errors when iterating