19
19
20
20
suppressMessages(library(Rcpp ))
21
21
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
+
22
27
suppressMessages(library(inline ))
23
28
24
- firstExample <- function () {
29
+ firstExample_old <- function () {
25
30
# # a really simple C program calling three functions from the GSL
26
31
gslrng <- '
27
32
gsl_rng *r;
@@ -41,16 +46,16 @@ firstExample <- function() {
41
46
42
47
# # turn into a function that R can call
43
48
# # 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" )
47
52
48
53
cat(" Calling first example\n " )
49
- funx ()
54
+ funx_old ()
50
55
invisible (NULL )
51
56
}
52
57
53
- secondExample <- function () {
58
+ secondExample_old <- function () {
54
59
55
60
# # now use Rcpp to pass down a parameter for the seed
56
61
gslrng <- '
@@ -78,27 +83,27 @@ secondExample <- function() {
78
83
# # turn into a function that R can call
79
84
# # compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
80
85
# # 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" )
84
89
cat(" \n\n Calling second example without -DBeSilent set\n " )
85
- print(funx (0 ))
90
+ print(funx_old (0 ))
86
91
87
92
88
93
# # now override settings to add -D flag
89
94
settings <- getPlugin(" RcppGSL" )
90
95
settings $ env $ PKG_CPPFLAGS <- paste(settings $ PKG_CPPFLAGS , " -DBeSilent" )
91
96
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 )
95
100
cat(" \n\n Calling second example with -DBeSilent set\n " )
96
- print(funx (0 ))
101
+ print(funx_old (0 ))
97
102
98
103
invisible (NULL )
99
104
}
100
105
101
- thirdExample <- function () {
106
+ thirdExample_old <- function () {
102
107
103
108
# # now use Rcpp to pass down a parameter for the seed, and a vector size
104
109
gslrng <- '
@@ -123,17 +128,17 @@ thirdExample <- function() {
123
128
# # turn into a function that R can call
124
129
# # compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
125
130
# # 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" )
130
135
cat(" \n\n Calling third example with seed and length\n " )
131
- print(funx (0 , 5 ))
136
+ print(funx_old (0 , 5 ))
132
137
133
138
invisible (NULL )
134
139
}
135
140
136
- fourthExample <- function () {
141
+ fourthExample_old <- function () {
137
142
138
143
# # now use Rcpp to pass down a parameter for the seed, and a vector size
139
144
gslrng <- '
@@ -158,15 +163,185 @@ fourthExample <- function() {
158
163
# # turn into a function that R can call
159
164
# # compileargs redundant on Debian/Ubuntu as gsl headers are found anyway
160
165
# # 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" )
167
172
cat(" \n\n Calling 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\n Calling 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\n Calling 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\n Calling third example with seed and length\n " )
168
302
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){
169
326
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\n Calling fourth example with seed, length and namespaces\n " )
343
+ print(funx(0 , 5 ))
344
+
170
345
invisible (NULL )
171
346
}
172
347
0 commit comments