Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Chris DuBois
For now I am using the following, which seems to work. I didn't really need all the functionality from sample(); I just needed to get a random index (but with non-uniform probabilities). It might be cleaner to use std::accumulate in rcategorical, but I got this working first. Also, rcategorical

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 18:01, Chris DuBois wrote: | Thanks Dirk.  Passing the addition function in the includes argument worked | great.   | | Also, one of the links from your suggested Google search led me to this | (working) example for std::accumulate: | | src <- 'NumericVector xx(x); | return wra

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Chris DuBois
Thanks Dirk. Passing the addition function in the includes argument worked great. Also, one of the links from your suggested Google search led me to this (working) example for std::accumulate: src <- 'NumericVector xx(x); return wrap( std::accumulate( xx.begin(), xx.end(), 0.0));' fx <- cxxfunct

Re: [Rcpp-devel] R.e. Rcpp equivalent of sample()?

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 19:41, Christian Gunning wrote: | > In the Rcpp vignettes, I found some of the R-like functions for | > sampling from distributions (runif, rnorm, etc), but I didn't see a function | > mimicking sample(). I checked the list of unit tests and didn't see it there | > either.  Have

[Rcpp-devel] R.e. Rcpp equivalent of sample()?

2011-05-14 Thread Christian Gunning
> In the Rcpp vignettes, I found some of the R-like functions for > sampling from distributions (runif, rnorm, etc), but I didn't see a function > mimicking sample(). I checked the list of unit tests and didn't see it there > either.  Have I missed it?  Is there a C++ function I should be using to

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 17:13, Chris DuBois wrote: | Thanks Dirk.  Quick newbie followup question: Can we put functions within the | inline C++ code?  The following doesn't compile, for example: | src <- ' | int addition (int a, int b) | { |   int r; |   r=a+b; |   return (r); | } | NumericVector xx(x);

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 16:33, Douglas Bates wrote: | You can check what is done in R itself in the R sources but I imagine that it | uses a uniform random variate converted to an integer through the cumulative | probabilities. The good news is that it is easy to create the cumulative | weights with the

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Chris DuBois
Thanks Dirk. Quick newbie followup question: Can we put functions within the inline C++ code? The following doesn't compile, for example: src <- ' int addition (int a, int b) { int r; r=a+b; return (r); } NumericVector xx(x); return(xx); ' testfun = cxxfunction(signature(x="numeric"),body=s

Re: [Rcpp-devel] Accessing matrix elements

2011-05-14 Thread Dirk Eddelbuettel
Hi Michael, On 14 May 2011 at 18:00, Michael Braun wrote: | Hi, Rcpp-friends: | | File this post under "rookie mistake, "educational illustration," or "feature request," but I noticed something in accessing elements from Rcpp matrices that I found somewhat curious. Consider the following code

Re: [Rcpp-devel] Matrix columns

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 23:12, Nick Sabbe wrote: | I?ve been trying the following, with the main part ripped from rcpp-quickref: | | SEXP dfr2Mat(SEXP dfr) A much easier way is to, say, call as.matrix() in R and then assign to an NumericMatrix object in C++. Another way is turn the Data.Frame i

Re: [Rcpp-devel] Matrix columns

2011-05-14 Thread Michael Braun
Nick: I had a similar problem a few weeks ago. The underscore is part of the Rcpp namespace. So I added using namespace Rcpp at the beginning of my function, and then all worked well. I can't tell from the compiler output if that is *exactly* what the problem is, but it's worth taking a look

[Rcpp-devel] Accessing matrix elements

2011-05-14 Thread Michael Braun
Hi, Rcpp-friends: File this post under "rookie mistake, "educational illustration," or "feature request," but I noticed something in accessing elements from Rcpp matrices that I found somewhat curious. Consider the following code, for which the objective is just to print elements of a matrix.

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Dirk Eddelbuettel
On 14 May 2011 at 14:11, Chris DuBois wrote: | I'm just getting my feet wet with some Rcpp.  I'm comfortable with R but new | to C++.  In the Rcpp vignettes, I found some of the R-like functions for | sampling from distributions (runif, rnorm, etc), but I didn't see a function | mimicking sample()

Re: [Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Douglas Bates
You can check what is done in R itself in the R sources but I imagine that it uses a uniform random variate converted to an integer through the cumulative probabilities. The good news is that it is easy to create the cumulative weights with the std::accumulate function. On May 14, 2011 4:12 PM, "Ch

[Rcpp-devel] Matrix columns

2011-05-14 Thread Nick Sabbe
Hi all, I've been trying the following, with the main part ripped from rcpp-quickref: SEXP dfr2Mat(SEXP dfr) { DataFrame df = dfr; int* dm = INTEGER( ::Rf_getAttrib( df, R_DimSymbol ) ) ; int rows = dm[0]; int cols = dm[1]; NumericMatrix re

[Rcpp-devel] Rcpp equivalent of sample()?

2011-05-14 Thread Chris DuBois
Hi all, I'm just getting my feet wet with some Rcpp. I'm comfortable with R but new to C++. In the Rcpp vignettes, I found some of the R-like functions for sampling from distributions (runif, rnorm, etc), but I didn't see a function mimicking sample(). I checked the list of unit tests and didn't

Re: [Rcpp-devel] Create and access several instances of a C++ class from R

2011-05-14 Thread Dirk Eddelbuettel
Soeren, On 13 May 2011 at 20:07, soeren.vo...@uzh.ch wrote: | Compilation with R CMD CHECK FOO fails with the following error in 00install.out: | | Error in dyn.load(file, DLLpath = DLLpath, ...) : | unable to load shared object '/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so': | dlopen

Re: [Rcpp-devel] Exposing constructors with same number of arguments with different types using Rcpp Modules

2011-05-14 Thread Jelmer Ypma
Excellent, thanks for the update. Jelmer On Sat, May 14, 2011 at 17:20, Dirk Eddelbuettel wrote: > > On 13 May 2011 at 11:58, Dirk Eddelbuettel wrote: > | On 13 May 2011 at 16:35, Jelmer Ypma wrote: > | | Dear Rcpp-list, > | | > | | I'm trying to expose multiple constructors of a C++ class to R

Re: [Rcpp-devel] Exposing constructors with same number of arguments with different types using Rcpp Modules

2011-05-14 Thread Dirk Eddelbuettel
On 13 May 2011 at 11:58, Dirk Eddelbuettel wrote: | On 13 May 2011 at 16:35, Jelmer Ypma wrote: | | Dear Rcpp-list, | | | | I'm trying to expose multiple constructors of a C++ class to R using | | modules. Here is an example of what I want to do based on the Uniform | | example [...] | | Compil