Question

When I use RStudio, I can see any errors or warnings when I run a function.

However when I am using, rpy2, how can I catch warnings (which allow the code to run) and errors (which stall the code) so I can parse the messages programmatically in python?

Was it helpful?

Solution

Until R-3.0 (current unreleased), warnings were printed to the console by default. From R-3-0, they are no longer printed because the R developers made the C function previously used hidden (and are too busy to document why and tell whether we could get access to that function back or not).

To get the warnings as an rpy2 object, you can do:

from rpy2.robjects.packages import importr
base = importr('base')
# do things that generate R warnings
base.warnings()

Errors occurring while evaluating R code raise an rpy2.rinterface.RRuntimeError. Just catch those.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top