You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//YouwillneedtoedittheRcppModules:lineoftheDESCRIPTIONfiletomatchyourmodule name (inthisexample, fromyadatomod_bar).
289
+
//Enable OpenMP (excludesmacOS)
290
+
// [[Rcpp::plugins(openmp)]]
274
291
275
-
classBar {
276
-
public:
277
-
Bar(doublex_) :
278
-
x(x_), nread(0), nwrite(0) {}
279
-
280
-
double get_x( ) {
281
-
nread++; returnx;
282
-
}
283
-
284
-
void set_x( doublex_) {
285
-
nwrite++; x=x_;
286
-
}
287
-
288
-
IntegerVector stats() const {
289
-
returnIntegerVector::create(
290
-
_["read"] =nread,
291
-
_["write"] =nwrite);
292
-
}
293
-
private:
294
-
doublex; intnread, nwrite;
295
-
};
296
-
297
-
RCPP_MODULE(mod_bar) {
298
-
class_<Bar>( "Bar" )
299
-
.constructor<double>()
300
-
.property( "x", &Bar::get_x, &Bar::set_x,
301
-
"Docstring for x" )
302
-
.method( "stats", &Bar::stats,
303
-
"Docstring for stats")
304
-
;}
305
-
306
-
## The following is R code.
307
-
require(mypackage); show(Bar)
308
-
b<- new(Bar, 10); b$x<-10
309
-
b_persist<-list(stats=b$stats(), x=b$x)
310
-
rm(b)
292
+
//UsetheRcppArmadillopackage
293
+
//RequiresdifferentheaderfilefromRcpp.h
294
+
#include <RcppArmadillo.h>
295
+
// [[Rcpp::depends(RcppArmadillo)]]
311
296
@
312
297
313
-
\newpage
298
+
314
299
315
300
\paragraph{Rcpp sugar}~
316
301
\newline
@@ -386,4 +371,121 @@ double zz = Rf_rnorm(0, 2);
386
371
387
372
388
373
374
+
\newpage
375
+
376
+
\paragraph{Environment}~
377
+
\newline
378
+
<<lang=cpp>>=
379
+
// Obtain an R environment
380
+
Environment stats("package:stats");
381
+
Environment env( 2 ); // by position
382
+
383
+
// Special environments
384
+
Environment::Rcpp_namespace();
385
+
Environment::base_env();
386
+
Environment::base_namespace();
387
+
Environment::global_env();
388
+
Environment::empty_env();
389
+
390
+
// Extract function from specific
391
+
// environment
392
+
Function rnorm = stats["rnorm"];
393
+
394
+
// Assign into the environment
395
+
glob["x"] = "foo";
396
+
glob["y"] = 3;
397
+
398
+
// Retrieve information from environment
399
+
std::string x = glob["x"];
400
+
glob.assign( "foo" , 3 );
401
+
int foo = glob.get( "foo" );
402
+
int foo = glob.find( "foo" );
403
+
CharacterVector names = glob.ls()
404
+
bool b = glob.exists( "foo" );
405
+
glob.remove( "foo" );
406
+
407
+
// Administration
408
+
glob.lockBinding("foo");
409
+
glob.unlockBinding("foo");
410
+
bool b = glob.bindingIsLocked("foo");
411
+
bool b = glob.bindingIsActive("foo");
412
+
413
+
// Retrieve related environments
414
+
Environment e = stats.parent();
415
+
Environment e = glob.new_child();
416
+
@
417
+
418
+
\paragraph{Calling Functions in R}~
419
+
\newline
420
+
<<lang=cpp>>=
421
+
// Do NOT expect to have a performance gain
422
+
// when calling R functions from R!
423
+
424
+
// Retrieve functions from default loaded environment
425
+
Function rnorm("rnorm");
426
+
rnorm(100, _["mean"] = 10.2, _["sd"] = 3.2 );
427
+
428
+
// Passing in an R function and obtaining results
429
+
// Make sure the function conforms with return type!
430
+
NumericVector callFunction(NumericVector x,
431
+
Function f) {
432
+
NumericVector res = f(x);
433
+
return res;
434
+
}
435
+
436
+
## In R:
437
+
x = 1:5
438
+
callFunction(x, sum)
439
+
@
440
+
441
+
\newpage
442
+
443
+
\paragraph{Modules}~
444
+
\newline
445
+
<<lang=cpp>>=
446
+
// Warning -- At present, module-based objects do not persist across quit(save="yes")/reload cycles. To be safe, save results to R objects and remove module objects before exiting R.
447
+
448
+
// To create a module-containing package from R, use:
449
+
Rcpp.package.skeleton("mypackage",module=TRUE)
450
+
// You will need to edit the RcppModules: line of the DESCRIPTION file to match your module name (in this example, from yada to mod_bar).
0 commit comments