Skip to content

Commit 30fe426

Browse files
committed
Modernizing RcppInline
Part of #56's push to switch examples away from the cxxfunction to Rcpp attributes.
1 parent 38b1f41 commit 30fe426

File tree

4 files changed

+298
-33
lines changed

4 files changed

+298
-33
lines changed

inst/examples/RcppInline/RcppInlineExample.r

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env r
22

33
suppressMessages(library(Rcpp))
4+
5+
## NOTE: This is the old way to compile Rcpp code inline.
6+
## The code here has left as a historical artifact and tribute to the old way.
7+
## Please use the code under the "new" inline compilation section.
8+
49
suppressMessages(library(inline))
510

611
foo <- '
@@ -15,7 +20,22 @@ foo <- '
1520
return vec;
1621
'
1722

18-
funx <- cxxfunction(signature(), foo, plugin = "Rcpp" )
23+
funx_old <- cxxfunction(signature(), foo, plugin = "Rcpp" )
24+
25+
## NOTE: Within this section, the new way to compile Rcpp code inline has been
26+
## written. Please use the code next as a template for your own project.
27+
28+
cppFunction('IntegerVector funx(){
29+
IntegerVector vec(10000); // vec parameter viewed as vector of ints.
30+
int i = 0;
31+
for (int a = 0; a < 9; a++)
32+
for (int b = 0; b < 9; b++)
33+
for (int c = 0; c < 9; c++)
34+
for (int d = 0; d < 9; d++)
35+
vec(i++) = a*b - c*d;
36+
37+
return vec;
38+
}')
1939

2040
dd.inline.rcpp <- function() {
2141
res <- funx()

inst/examples/RcppInline/RcppInlineWithLibsExamples.r

Lines changed: 202 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919

2020
suppressMessages(library(Rcpp))
2121
suppressMessages(library(RcppGSL))
22+
23+
## NOTE: This is the old way to compile Rcpp code inline.
24+
## The code here has left as a historical artifact and tribute to the old way.
25+
## Please use the code under the "new" inline compilation section.
26+
2227
suppressMessages(library(inline))
2328

24-
firstExample <- function() {
29+
firstExample_old <- function() {
2530
## a really simple C program calling three functions from the GSL
2631
gslrng <- '
2732
gsl_rng *r;
@@ -41,16 +46,16 @@ firstExample <- function() {
4146

4247
## turn into a function that R can call
4348
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
44-
funx <- cxxfunction(signature(), gslrng,
45-
includes="#include <gsl/gsl_rng.h>",
46-
plugin="RcppGSL")
49+
funx_old <- cxxfunction(signature(), gslrng,
50+
includes="#include <gsl/gsl_rng.h>",
51+
plugin="RcppGSL")
4752

4853
cat("Calling first example\n")
49-
funx()
54+
funx_old()
5055
invisible(NULL)
5156
}
5257

53-
secondExample <- function() {
58+
secondExample_old <- function() {
5459

5560
## now use Rcpp to pass down a parameter for the seed
5661
gslrng <- '
@@ -78,27 +83,27 @@ secondExample <- function() {
7883
## turn into a function that R can call
7984
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
8085
## use additional define for compile to suppress output
81-
funx <- cxxfunction(signature(par="numeric"), gslrng,
82-
includes="#include <gsl/gsl_rng.h>",
83-
plugin="RcppGSL")
86+
funx_old <- cxxfunction(signature(par="numeric"), gslrng,
87+
includes="#include <gsl/gsl_rng.h>",
88+
plugin="RcppGSL")
8489
cat("\n\nCalling second example without -DBeSilent set\n")
85-
print(funx(0))
90+
print(funx_old(0))
8691

8792

8893
## now override settings to add -D flag
8994
settings <- getPlugin("RcppGSL")
9095
settings$env$PKG_CPPFLAGS <- paste(settings$PKG_CPPFLAGS, "-DBeSilent")
9196

92-
funx <- cxxfunction(signature(par="numeric"), gslrng,
93-
includes="#include <gsl/gsl_rng.h>",
94-
settings=settings)
97+
funx_old <- cxxfunction(signature(par="numeric"), gslrng,
98+
includes="#include <gsl/gsl_rng.h>",
99+
settings=settings)
95100
cat("\n\nCalling second example with -DBeSilent set\n")
96-
print(funx(0))
101+
print(funx_old(0))
97102

98103
invisible(NULL)
99104
}
100105

101-
thirdExample <- function() {
106+
thirdExample_old <- function() {
102107

103108
## now use Rcpp to pass down a parameter for the seed, and a vector size
104109
gslrng <- '
@@ -123,17 +128,17 @@ thirdExample <- function() {
123128
## turn into a function that R can call
124129
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
125130
## use additional define for compile to suppress output
126-
funx <- cxxfunction(signature(s="numeric", n="numeric"),
127-
gslrng,
128-
includes="#include <gsl/gsl_rng.h>",
129-
plugin="RcppGSL")
131+
funx_old <- cxxfunction(signature(s="numeric", n="numeric"),
132+
gslrng,
133+
includes="#include <gsl/gsl_rng.h>",
134+
plugin="RcppGSL")
130135
cat("\n\nCalling third example with seed and length\n")
131-
print(funx(0, 5))
136+
print(funx_old(0, 5))
132137

133138
invisible(NULL)
134139
}
135140

136-
fourthExample <- function() {
141+
fourthExample_old <- function() {
137142

138143
## now use Rcpp to pass down a parameter for the seed, and a vector size
139144
gslrng <- '
@@ -158,15 +163,185 @@ fourthExample <- function() {
158163
## turn into a function that R can call
159164
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
160165
## use additional define for compile to suppress output
161-
funx <- cxxfunction(signature(s="numeric", n="numeric"),
162-
gslrng,
163-
includes=c("#include <gsl/gsl_rng.h>",
164-
"using namespace Rcpp;",
165-
"using namespace std;"),
166-
plugin="RcppGSL")
166+
funx_old <- cxxfunction(signature(s="numeric", n="numeric"),
167+
gslrng,
168+
includes=c("#include <gsl/gsl_rng.h>",
169+
"using namespace Rcpp;",
170+
"using namespace std;"),
171+
plugin="RcppGSL")
167172
cat("\n\nCalling fourth example with seed, length and namespaces\n")
173+
print(funx_old(0, 5))
174+
175+
invisible(NULL)
176+
}
177+
178+
## NOTE: Within this section, the new way to compile Rcpp code inline has been
179+
## written. Please use the code next as a template for your own project.
180+
181+
firstExample <- function() {
182+
## a really simple C program calling three functions from the GSL
183+
184+
sourceCpp(code='
185+
#include <RcppGSL.h>
186+
#include <gsl/gsl_rng.h>
187+
188+
// [[Rcpp::depends(RcppGSL)]]
189+
190+
// [[Rcpp::export]]
191+
SEXP funx(){
192+
gsl_rng *r;
193+
gsl_rng_env_setup();
194+
double v;
195+
196+
r = gsl_rng_alloc (gsl_rng_default);
197+
198+
printf(" generator type: %s\\n", gsl_rng_name (r));
199+
printf(" seed = %lu\\n", gsl_rng_default_seed);
200+
v = gsl_rng_get (r);
201+
printf(" first value = %.0f\\n", v);
202+
203+
gsl_rng_free(r);
204+
return R_NilValue;
205+
}')
206+
207+
cat("Calling first example\n")
208+
funx()
209+
invisible(NULL)
210+
}
211+
212+
secondExample <- function() {
213+
214+
## now use Rcpp to pass down a parameter for the seed
215+
216+
## turn into a function that R can call
217+
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
218+
## use additional define for compile to suppress output
219+
220+
gslrng <- '
221+
#include <RcppGSL.h>
222+
#include <gsl/gsl_rng.h>
223+
224+
// [[Rcpp::depends(RcppGSL)]]
225+
226+
// [[Rcpp::export]]
227+
double funx(int seed){
228+
229+
gsl_rng *r;
230+
gsl_rng_env_setup();
231+
double v;
232+
233+
r = gsl_rng_alloc (gsl_rng_default);
234+
235+
gsl_rng_set (r, (unsigned long) seed);
236+
v = gsl_rng_get (r);
237+
238+
#ifndef BeSilent
239+
printf(" generator type: %s\\n", gsl_rng_name (r));
240+
printf(" seed = %d\\n", seed);
241+
printf(" first value = %.0f\\n", v);
242+
#endif
243+
244+
gsl_rng_free(r);
245+
return v;
246+
}'
247+
248+
sourceCpp(code=gslrng, rebuild = TRUE)
249+
250+
cat("\n\nCalling second example without -DBeSilent set\n")
251+
print(funx(0))
252+
253+
254+
## now override settings to add -D flag
255+
o = Sys.getenv("PKG_CPPFLAGS")
256+
Sys.setenv("PKG_CPPFLAGS" = paste(o, "-DBeSilent"))
257+
258+
sourceCpp(code=gslrng, rebuild = TRUE)
259+
260+
# Restore environment flags
261+
Sys.setenv("PKG_CPPFLAGS" = o )
262+
263+
cat("\n\nCalling second example with -DBeSilent set\n")
264+
print(funx(0))
265+
266+
invisible(NULL)
267+
}
268+
269+
thirdExample <- function() {
270+
271+
## now use Rcpp to pass down a parameter for the seed, and a vector size
272+
273+
## turn into a function that R can call
274+
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
275+
## use additional define for compile to suppress output
276+
277+
sourceCpp(code='
278+
#include <RcppGSL.h>
279+
#include <gsl/gsl_rng.h>
280+
281+
// [[Rcpp::depends(RcppGSL)]]
282+
283+
// [[Rcpp::export]]
284+
std::vector<double> funx(int seed, int len){
285+
286+
gsl_rng *r;
287+
gsl_rng_env_setup();
288+
std::vector<double> v(len);
289+
290+
r = gsl_rng_alloc (gsl_rng_default);
291+
292+
gsl_rng_set (r, (unsigned long) seed);
293+
for (int i=0; i<len; i++) {
294+
v[i] = gsl_rng_get (r);
295+
}
296+
gsl_rng_free(r);
297+
298+
return v;
299+
}')
300+
301+
cat("\n\nCalling third example with seed and length\n")
168302
print(funx(0, 5))
303+
304+
invisible(NULL)
305+
}
306+
307+
fourthExample <- function() {
308+
309+
## now use Rcpp to pass down a parameter for the seed, and a vector size
310+
311+
## turn into a function that R can call
312+
## compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
313+
## use additional define for compile to suppress output
314+
315+
sourceCpp(code='
316+
#include <RcppGSL.h>
317+
#include <gsl/gsl_rng.h>
318+
319+
using namespace Rcpp;
320+
using namespace std;
321+
322+
// [[Rcpp::depends(RcppGSL)]]
323+
324+
// [[Rcpp::export]]
325+
std::vector<double> funx(int seed, int len){
169326
327+
gsl_rng *r;
328+
gsl_rng_env_setup();
329+
std::vector<double> v(len);
330+
331+
r = gsl_rng_alloc (gsl_rng_default);
332+
333+
gsl_rng_set (r, (unsigned long) seed);
334+
for (int i=0; i<len; i++) {
335+
v[i] = gsl_rng_get (r);
336+
}
337+
gsl_rng_free(r);
338+
339+
return v;
340+
}')
341+
342+
cat("\n\nCalling fourth example with seed, length and namespaces\n")
343+
print(funx(0, 5))
344+
170345
invisible(NULL)
171346
}
172347

inst/examples/RcppInline/UncaughtExceptions.r

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,26 @@
1818
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
1919

2020
require(Rcpp)
21+
22+
23+
## NOTE: This is the old way to compile Rcpp code inline.
24+
## The code here has left as a historical artifact and tribute to the old way.
25+
## Please use the code under the "new" inline compilation section.
26+
2127
require(inline)
22-
funx <- cxxfunction(
28+
funx_old <- cxxfunction(
2329
signature(),
2430
'throw std::range_error("boom"); return R_NilValue ; ',
2531
plugin = "Rcpp" )
32+
33+
## NOTE: Within this section, the new way to compile Rcpp code inline has been
34+
## written. Please use the code next as a template for your own project.
35+
36+
cppFunction('
37+
SEXP funx(){
38+
throw std::range_error("boom"); return R_NilValue ;
39+
}')
40+
2641
tryCatch( funx(), "C++Error" = function(e){
2742
cat( sprintf( "C++ exception of class '%s' : %s\n", class(e)[1L], e$message ) )
2843
} )

0 commit comments

Comments
 (0)