Alternative question: RStudio and Jupyter Notebook produces different output
This may look like too trivial QA, but if debug is not easily available within R binary package, it's not a prior knowledge that library actually used stderr instead of expected stdout.
Under raw R (and RStudio) library function prints just progress bar:
....
While under rpy2.ipython (which can be used from Jupyter Notebook) same function outputs extra message and newlines:
R[write to console]: .
R[write to console]: .
R[write to console]: .
R[write to console]: .
What is the reason and how to fix?
It appears to be rpy2.ipython
adds extra message when stderr
is used under R.
Redirecting to stdout
is one of possible fixes:
sink(stdout(), type = "message")
# call library function here
And just in case - for complicated debug cases, mocking output may be handy. There must be a better way to keep base::cat
than simply copying it's code, but this worked well for diagnostics too:
original_cat <- function(..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE){
if (is.character(file))
if (file == "")
file <- stdout()
else if (startsWith(file, "|")) {
file <- pipe(substring(file, 2L), "w")
on.exit(close(file))
}
else {
file <- file(file, ifelse(append, "a", "w"))
on.exit(close(file))
}
.Internal(cat(list(...), file, sep, fill, labels, append))
}
cat_ex <- function(...){
# original_cat(...)
args <- list(...)
if (!is.null(args[["file"]]))
args[["file"]] <- NULL
do.call(original_cat, args)
}
library(testthat)
with_mock(
cat = cat_ex,
# call library function here
)