[Rcpp-devel] Beginner Question: Two misc. questions on usage of Rcpp

Christian Gunning xian at unm.edu
Tue Feb 1 01:22:08 CET 2011

> 2. I have a MCMC simulation (say: foo) which uses several functions (say:
> fun1, fun2). These functions (fun1, fun2) are themselves computation
> intensive and I used Rcpp to speed them. Until now I do something like this:
> In R file:
> foo <- function (...){
>    src1 <- paste(readLines("fun1-rcpp.cpp"), collapse="\n")
>    RcppFun1 <- cxxfunction( .., plugin="Rcpp",body=src1)
>   ## similar cpp code for fun2
>    for (i in 1:10000){
>          ## use RcppFun1, RcppFun2 here
>    }
> }

For MCMC, independence of loop iterations argues for explicit
parallelism.  Assuming you have several cores available, look at the
foreach package.  If you have several *machines*, each with many cores
available, something like the snowfall package might make sense,
depending on communication costs.


It's "yet another new thing", but as Dirk said, creating a package
should help a lot for "real work". See ?Rcpp.package.skeleton to get
started, and the "Writing R Extensions" manual for details.  Exactly
how to do this depends on OS (I don't know about windows shell), but
the overview is:

## 1. Start R in directory to contain myPackage and run the following
> Rcpp.package.skeleton("myPackage")

## 2. edit code in myPackage/R and myPackage/src directories

## 3. Run following commands from your OS shell,
## in the directory from step 1 (just above the package directory)
R CMD check myPackage  ## Optional

## 4. Start R from any directory and do
> require(myPackage)

## 5. After editing code, you must exit R and repeat steps 3 and 4
## to reload your package.

A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama!

More information about the Rcpp-devel mailing list