[Rcpp-devel] NumericVector Double mismatch when indexing an array
Goldfeld, Keith
Keith.Goldfeld at nyumc.org
Tue Sep 25 20:37:29 CEST 2012
I am a new Rcpp user, and I have been trying to do a simple exercise of generating random numbers to put into a vector. To start off, the following code works:
> code <- 'Rcpp::RNGScope scope;
+ Rcpp::NumericVector rn(5);
+ rn = rnorm(5,0,1);
+ return Rcpp::wrap(rn);
+ '
>
> fun <- cxxfunction(body=code, plugin="Rcpp")
cygwin warning:
MS-DOS style path detected: C:/R/R-215~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-215~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
>
> (res <- fun())
[1] 1.7517515 0.3178733 -0.6957581 1.7030360 -1.6546936
When I try to do the same thing, but in a loop as shown here:
> 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")
the result is an error message indicating that it cannot convert a NumericVector to double. Since in the first example I did not index, I am assuming that I have indexed incorrectly, but I can't see how. Below is the detailed error message - any idea what I am doing wrong?
cygwin warning:
MS-DOS style path detected: C:/R/R-215~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-215~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
file15182ecd2ee.cpp: In function 'SEXPREC* file15182ecd2ee()':
file15182ecd2ee.cpp:33:31: error: cannot convert 'Rcpp::NumericVector {aka Rcpp::Vector<14>}' to 'Rcpp::traits::storage_type<14>::type {aka double}' in assignment
make: *** [file15182ecd2ee.o] Error 1
ERROR(s) during compilation: source code errors or compiler configuration errors!
Program source:
1:
2: // includes from the plugin
3:
4: #include <Rcpp.h>
5:
6:
7: #ifndef BEGIN_RCPP
8: #define BEGIN_RCPP
9: #endif
10:
11: #ifndef END_RCPP
12: #define END_RCPP
13: #endif
14:
15: using namespace Rcpp;
16:
17:
18: // user includes
19:
20:
21: // declarations
22: extern "C" {
23: SEXP file15182ecd2ee( ) ;
24: }
25:
26: // definition
27:
28: SEXP file15182ecd2ee( ){
29: BEGIN_RCPP
30: Rcpp::RNGScope scope;
31: Rcpp::NumericVector rn(5);
32: for (int i=0; i < 5; i++) {
33: rn(i) = rnorm(1,0,1);
34: }
35: return Rcpp::wrap(rn);
36:
37: END_RCPP
38: }
39:
40:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! cygwin warning:
MS-DOS style path detected: C:/R/R-215~1.1/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R/R-215~1.1/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
file15182ecd2ee.cpp: In function 'SEXPREC* file15182ecd2ee()':
file15182ecd2ee.cpp:33:31: error: cannot convert 'Rcpp::NumericVector {aka Rcpp::Vector<14>}' to 'Rcpp::traits::storage_type<14>::type {aka double}' in assignment
make: *** [file15182ecd2ee.o] Error 1
In addition: Warning message:
running command 'C:/R/R-215~1.1/bin/i386/R CMD SHLIB file15182ecd2ee.cpp 2> file15182ecd2ee.cpp.err.txt' had status 1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120925/e263abed/attachment.html>
More information about the Rcpp-devel
mailing list