Skip to content

Commit 88f8f8e

Browse files
committed
new post on using Rcout (and Rcerr)
1 parent 44be20e commit 88f8f8e

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

_posts/2013-01-08-using-rcout.md

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: Using Rcout for output synchronised with R
3+
author: Dirk Eddelbuettel
4+
license: GPL (>= 2)
5+
tags: basics featured
6+
summary: This post shows how to use Rcout (and Rcerr) for output
7+
layout: post
8+
src: 2013-01-08-using-rcout.cpp
9+
---
10+
11+
The [Writing R Extensions](http://cran.r-project.org/doc/manuals/R-exts.html) manual,
12+
which provides the gold standard of documentation as far as
13+
extending R goes, suggests to use `Rprintf` and `REprintf` for
14+
output (from C/C++ code) as these are matched to the usual output and error streams
15+
maintained by R itself.
16+
17+
Also, use of `std::cout` and `std::cerr` (as common in standard C++ code) is flagged when
18+
running `R CMD check` and no longer permitted when uploading to CRAN.
19+
20+
Thanks to an initial patch by Jelmer Ypma, which has since been
21+
reworked and extended, we have devices `Rcout` (for standard
22+
output) and `Rcerr` (for standard error) which intercept output and
23+
redirect it to R.
24+
25+
To illustrate, we create a simple function which prints a value:
26+
27+
28+
29+
{% highlight cpp %}
30+
#include <RcppArmadillo.h> // as we use RcppArmadillo below
31+
// this first example use only Rcpp
32+
33+
using namespace Rcpp;
34+
35+
// [[Rcpp::export]]
36+
void showValue(double x) {
37+
Rcout << "The value is " << x << std::endl;
38+
}
39+
{% endhighlight %}
40+
41+
42+
We can use this from R, and output will be properly synchronised:
43+
44+
{% highlight r %}
45+
cat("Before\n")
46+
{% endhighlight %}
47+
48+
49+
50+
<pre class="output">
51+
Before
52+
</pre>
53+
54+
55+
56+
{% highlight r %}
57+
showValue(1.23)
58+
{% endhighlight %}
59+
60+
61+
62+
<pre class="output">
63+
The value is 1.23
64+
</pre>
65+
66+
67+
68+
{% highlight r %}
69+
cat("After\n")
70+
{% endhighlight %}
71+
72+
73+
74+
<pre class="output">
75+
After
76+
</pre>
77+
78+
79+
As of the 0.10.* releases, Rcpp itself still lacks the converter code to
80+
print simple non-scalar data structures---but RcppArmadillo can do
81+
so as Conrad permitted a hool for us to supply the Rcout device as
82+
the default device
83+
84+
{% highlight cpp %}
85+
#include <RcppArmadillo.h>
86+
87+
// [[Rcpp::depends(RcppArmadillo)]]
88+
89+
// [[Rcpp::export]]
90+
void showMatrix(arma::mat X) {
91+
Rcout << "Armadillo matrix is" << std::endl << X << std::endl;
92+
}
93+
{% endhighlight %}
94+
95+
96+
{% highlight r %}
97+
M <- matrix(1:9,3,3)
98+
print(M)
99+
{% endhighlight %}
100+
101+
102+
103+
<pre class="output">
104+
[,1] [,2] [,3]
105+
[1,] 1 4 7
106+
[2,] 2 5 8
107+
[3,] 3 6 9
108+
</pre>
109+
110+
111+
112+
{% highlight r %}
113+
showMatrix(M)
114+
{% endhighlight %}
115+
116+
117+
118+
<pre class="output">
119+
Armadillo matrix is
120+
1.0000 4.0000 7.0000
121+
2.0000 5.0000 8.0000
122+
3.0000 6.0000 9.0000
123+
</pre>
124+
125+
126+
Having output from R and C++ mix effortlessly is a very useful
127+
feature. We hope to over time add more features to output more of
128+
Rcpp basic objects. Patches are of course always welcome.

src/2013-01-08-using-rcout.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
/**
3+
* @title Using Rcout for output synchronised with R
4+
* @author Dirk Eddelbuettel
5+
* @license GPL (>= 2)
6+
* @tags basics featured
7+
* @summary This post shows how to use Rcout (and Rcerr) for output
8+
*
9+
10+
* The [Writing R Extensions](http://cran.r-project.org/doc/manuals/R-exts.html) manual,
11+
* which provides the gold standard of documentation as far as
12+
* extending R goes, suggests to use `Rprintf` and `REprintf` for
13+
* output (from C/C++ code) as these are matched to the usual output and error streams
14+
* maintained by R itself.
15+
*
16+
* Also, use of `std::cout` and `std::cerr` (as common in standard C++ code) is flagged when
17+
* running `R CMD check` and no longer permitted when uploading to CRAN.
18+
*
19+
* Thanks to an initial patch by Jelmer Ypma, which has since been
20+
* reworked and extended, we have devices `Rcout` (for standard
21+
* output) and `Rcerr` (for standard error) which intercept output and
22+
* redirect it to R.
23+
*
24+
* To illustrate, we create a simple function which prints a value:
25+
*/
26+
27+
#include <RcppArmadillo.h> // as we use RcppArmadillo below
28+
// this first example use only Rcpp
29+
30+
using namespace Rcpp;
31+
32+
// [[Rcpp::export]]
33+
void showValue(double x) {
34+
Rcout << "The value is " << x << std::endl;
35+
}
36+
37+
/**
38+
* We can use this from R, and output will be properly synchronised:
39+
*/
40+
41+
/*** R
42+
cat("Before\n")
43+
showValue(1.23)
44+
cat("After\n")
45+
*/
46+
47+
/**
48+
* As of the 0.10.* releases, Rcpp itself still lacks the converter code to
49+
* print simple non-scalar data structures---but RcppArmadillo can do
50+
* so as Conrad permitted a hool for us to supply the Rcout device as
51+
* the default device
52+
*/
53+
54+
#include <RcppArmadillo.h>
55+
56+
// [[Rcpp::depends(RcppArmadillo)]]
57+
58+
// [[Rcpp::export]]
59+
void showMatrix(arma::mat X) {
60+
Rcout << "Armadillo matrix is" << std::endl << X << std::endl;
61+
}
62+
63+
/*** R
64+
M <- matrix(1:9,3,3)
65+
print(M)
66+
showMatrix(M)
67+
*/
68+
69+
/**
70+
* Having output from R and C++ mix effortlessly is a very useful
71+
* feature. We hope to over time add more features to output more of
72+
* Rcpp basic objects. Patches are of course always welcome.
73+
*/

0 commit comments

Comments
 (0)