Dirk Eddelbuettel — written Jan 8, 2013 — source
The Writing R Extensions
manual, which provides the gold standard of documentation as far as
extending R goes, strongly suggests to use Rprintf
and REprintf
for output (from C/C++ code). The key reason is that these are
matched to the usual output and error streams maintained by R
itself.
In fact, use of std::cout
and std::cerr
(as common in standard
C++ code) is flagged when running R CMD check
and no longer
permitted when uploading to CRAN.
Thanks to an initial patch by Jelmer Ypma, which has since been
reworked and extended, we have devices Rcout
(for standard
output) and Rcerr
(for standard error) which intercept output and
redirect it to R.
To illustrate, we create a simple function which prints a value:
We can use this from R, and output will be properly synchronised:
Before
The value is 1.23
After
During the 0.10.* abd 0.11.* releases, Rcpp itself still lacked the
converter code to “pretty-print simple non-scalar data
structures. But there always were alternatives. First,
RcppArmadillo can do so via its operator<<()
as the
(Rcpp)Armadillo output is automatically redirected to R output
stream. See below for recent alternatives from Rcpp itself.
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
Armadillo matrix is 1.0000 4.0000 7.0000 2.0000 5.0000 8.0000 3.0000 6.0000 9.0000
Having output from R and C++ mix effortlessly is a very useful feature. We hope to over time add more features to output more of Rcpp basic objects.
Alternatively, starting with R version 0.11.5, we now have function
print()
which can print any SEXP
object – by calling the internal R
function Rf_PrintValue()
.
A simple illustration follow. We first define helper function.
A few examples calls follow below.
[1] 1 2 3
[1] "A" "B" "C"
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
<environment: R_GlobalEnv>
Starting with version 0.12.1 of Rcpp, the operator<<()
is also suppported for
vector and matrix types. See below for some examples.
Rcpp vector is 0 2.5 5 7.5 10
Rcpp matrix is 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
tags: basics
Tweet