[Rcpp-devel] NumericVector Double mismatch when indexing an array

Rodney Sparapani rsparapa at mcw.edu
Tue Sep 25 20:53:23 CEST 2012


On 09/25/2012 01:37 PM, Goldfeld, Keith wrote:

>>  code<- 'Rcpp::RNGScope scope;
>
> +                    Rcpp::NumericVector rn(5);
>
> +                    for (int i=0; i<  5; i++) {
>
> +                        rn(i) = rnorm(1,0,1);
>
> +                    }
>
> +                    return Rcpp::wrap(rn);
>
> +  '
>
>>
>
>>  fun<- cxxfunction(body=code, plugin="Rcpp")

You just need an explicit type conversion like so:

funk2 <- cxxfunction(body='RNGScope scope;
           NumericVector rn(5);
           for (int i=0; i < 5; i++) rn[i] = as<double>(rnorm(1,0,1));
           return wrap(rn);',
           includes="using namespace Rcpp;\n", plugin="Rcpp")

funk2()


-- 
Rodney Sparapani, PhD  Center for Patient Care and Outcomes Research
Sr. Biostatistician               http://www.mcw.edu/pcor
4 wheels good, 2 wheels better!   Medical College of Wisconsin (MCW)
WWLD?:  What Would Lombardi Do?   Milwaukee, WI, USA


More information about the Rcpp-devel mailing list