[Rcpp-devel] Sample function(s) for inclusion in RcppArmadillo

Christian Gunning xian at unm.edu
Tue Dec 25 09:39:02 CET 2012


I'm working on writing runit tests for the previously-discussed
sample, and I just hit my competence threshold dealing with
inst/include.
As discussed, the (overloaded) sample() functions are to be called
only from user C++ code and are thus not exposed to R.
This means that the function definitions should go in inst/include, correct?

I don't really know what I'm doing here.  If someone could recommend a
good reference for namespace control, headers, and R-package specific
issues I'd be grateful.   Details follow:

I'm trying to get the following test function to use RcppArmadillo's
definition of sample, and the compiler never finds it:
    require(Rcpp)
    cppFunction( code='
        IntegerVector sample_int_noprob( IntegerVector x, int size,
bool replace) {
            RNGScope scope;
            IntegerVector ret = RcppArmadillo::sample(x, size, replace);
            return ret;
        }',
    depends='RcppArmadillo'
    )

I tried placing the definition of sample in
RcppArmadillo/inst/include/sample.h.
        using namespace Rcpp;
        template <class T>
        T sample(const T &x, const int size, const bool replace,
NumericVector prob_ = NumericVector(0) ) {
            // Templated sample -- should work on any Rcpp Vector
         ...
        }

I've tried to use RcppArmadilloWrap.h as an example, using:
namespace Rcpp{
    namespace RcppArmadillo{
    ...
    }
}

best,
Christian


On Thu, Nov 15, 2012 at 5:35 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 15 November 2012 at 04:00, Christian Gunning wrote:
> |     |     Also, you may need to add RNGScope() if you use R's RNG.
> |     |
> |     |
> |     |
> |     | Yes, I should have mentioned that in my post.  Calling RNGScope() is left
> |     to
> |     | the user, since multiple calls to sample() can conceivably happen inside
> |     a
> |     | single RNGScope().
> |
> |     With that, maybe best to be included as another example in RcppArmadillo ?
> |
> |
> | Why?  The proposed behavior is the same as sugar's rnorm, etc.: a utility
> | function that uses R's RNG -- it's the
> | user's responsibility to properly scope the RNG.
>
> Fair point.
>
> | When time permits, I'll add Romain's comments on sugarizing sample (which I
> | *think* I understand), along with some testing code.
>
> Excellent plan.
>
> Dirk
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
>



-- 
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