Trapping R Errors in rpy2 - r

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?

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.

Related

libstableR library unwanted output

I'm using libstableR library to calculate some estimators and function stable_fit_mle outputs some messages while running, namely tons of Minimizer warning: No error and occasionally also: FINAL ESTIMATED PARAMETER ARE NOT VALID: No error.
I managed to suppress those messages in my final document (compiled with R Sweave) with invisible(capture.output(...)) but those still clutter my console during compilation. Is there any way to stop those messages from appearing in the first place?
My main concern is to understand why stable_fit_mle is outputing these warnings (in oppose to similrar functions from libstableR package) and how to prevent this behaviour.

kerasR giving error

I am trying to use kerasR for deep learning in R. I am trying to reproduce the examples in the package. Trying the following code produces error:
library(kerasR)
mod <- Sequential()
The error is:
Error in Sequential() : attempt to apply non-function
I'd suggest to look at this issue in KerasR Github repo:
https://github.com/statsmaths/kerasR/issues/1
Basically you should check where is located your version of python and then use reticulate::use_python("PATH_TO_PYTHON") to tell the system where to find Python.
Watch Out!
You can load just one Python interpreter per session and the use_python() function doesn't warn you if there already is a loaded interpreter.
Moreover if you run py_config() it automatically loads the first interpreter that he finds (which, in your case, seems to be the wrong one!), thus you'd better call reticulate::use_python("PATH_TO_PYTHON") before anything else.

Why is source speed different from RStudio console line code?

I have a script with self-written functions (no plots). When I copy-paste that script into the R-Studio console, it takes ages to execute, but when I use source("Helperfunctions.R") it doesn't take more than a second.
Question: Where does the difference in speed come from?
I am aware of two differences between running code via the source() function vs. entering code at the R-Studio console:
From ?source:
Since expressions are not executed at the top level, auto-printing is not done.
The way I understand this: source() will not plot graphs (unless made specific with e.g. print(plot)), while the R Studio console codes will always plot graphs. I'm sure this will affect the speed of execution to a certain degree, but this seems irrelevant in my case, because there are barely any plot calls.
And:
(...) the complete file is parsed before any of it is run
I have been working with R for a while now, but I'm not sure whether this relevant for the speed-issue I'm having. Is it possible that completely parsing all code "before any of it is run" speeds up the execution of my helper functions script by a factor of a hundred?
Edit: I'm using R version 3.2.3.
The issue is not source() vs. console line code. Instead, it is an issue of how RStudio sends code from the source pane to the console.
When I copy the content of Helperfunctions.R and run it in RGui (instead of RStudio), the code is executed with nearly the same speed as when I use source("Helperfunctions.R") in RStudio.
Apparently, lines of code always (?) require more execution time in RStudio than in RGui. Even though you may usually not notice the time-difference when executing a couple of lines in the console, it seems to make a huge difference when, say, 3.000 lines of code are being executed in the R Studio console at once.
My understanding is that upon using source("Helperfunctions.R") in the RStudio source pane, the code is not actually sent to the RStudio console (which would have been slow), but is actually executed directly in the R language.

Setting log levels in RdotNet

I am trying to use the R {forecast} package on Windows using RDotNet.
Question: Is there a way to control the level of logging output across RDotNet/R?
For example, executing the RdotNet code
var accuracy = rengine.Evaluate("accuracy(fcst)").AsNumeric();
causes the result of evaluation to get logged to console (or log file) as well.
Is there a way to control this either in RDotNet or R or {forecast} ?
(I had tried this sometime ago using R 3.1.1 and RdotNet 1.5.15. Recently I upgraded to the latest - R 3.2.2 and RdotNet 1.6.5. I don't recall seeing this in the previous versions.)
The REngine object has a property AutoPrint, set to true by default (mirroring the default behavior of R). Setting it to false will not print things out unless explicitly requested via the print function (I think, as I recall).
You can look at this sample code to see it used. Also, a recent discussion touched on this.

Where can I find a log of R session warnings?

One of my R scripts produced a message that there are some warnings during processing. However, since this is not an interactive session, I can't use warnings() to access the warnings. What is the standard location, if any, of the most recent R session's warnings log file, so that I could review them? Thank you!
From ?warnings:
It is undocumented where last.warning is stored nor that it is
visible, and this is subject to change.
However, you can use the function warnings in your script and specify a file, where the last warnings should be saved.
warnings(file = "C:/Rwarnings.txt")

Resources