[Rcpp-devel] error while incompatible types in assignment

Dirk Eddelbuettel edd at debian.org
Wed Mar 23 21:27:07 CET 2011


On 24 March 2011 at 01:27, nandan amar wrote:
| Hello,
| I tried following code for copying data from an R array to C++ array variable
| ------------------------------------------------------------------
| #include <R.h>
| #include <R_ext/Applic.h>
| #include <Rinternals.h>
| #include <RInside.h> 
| int main(int argc, char *argv[])
|     {
| 
|     RInside R(argc, argv);
|     float sum[50];//int sum[50];
|      R.parseEvalQ("arr<-c(1:50)") ;
|     for(int i=0;i<=50;i++)
|     sum[i] = R["arr[i]"];    
|     for(int i=0;i<=50;i++)
|     std::cout << "value: " << sum[i] << std::endl ;
|     exit(0);
|     }
| -------------------------------------------------------------------------------
| It got compiled and I got following error while running. Error is same for sum
| [] to be int/float
| 
| terminate called after throwing an instance of 'Rcpp::not_compatible'
|   what():  expecting a single value
| Aborted

You had a couple of small errors in there (indices from 0 to 50) as well as
one design issue: R _and_ C++ (when using STL) know vectors, so why do this
element-wise?  Lastly, your 'i' is only known in C++ but not in R so it can
never index.

Below are two different fixed versions, each running just to 5 rather than 50.

Cheers, Dirk


edd at max:~/svn/rinside/pkg/inst/examples/standard$ make
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I"/usr/local/lib/R/site-library/RInside/include" -O3 -pipe -g -Wall    nandan2.cpp  -L/usr/lib64/R/lib -lR  -lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o nandan2
g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I"/usr/local/lib/R/site-library/RInside/include" -O3 -pipe -g -Wall    nandan.cpp  -L/usr/lib64/R/lib -lR  -lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o nandan
edd at max:~/svn/rinside/pkg/inst/examples/standard$ ./nandan; cat nandan.cpp
value: 1
value: 2
value: 3
value: 4
value: 5
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-

#include <RInside.h>

int main(int argc, char *argv[]) {

    RInside R(argc, argv);
    float sum[5];//int sum[50];
    R.parseEvalQ("arr <- c(1:50)") ;
    for(int i=1;i<=5;i++) {
        char txt[32];
        sprintf(txt, "z <- arr[%d]; z", i);
        sum[i-1] = R.parseEval(txt);;
    }
    for(int i=0;i<5;i++)
        std::cout << "value: " << sum[i] << std::endl ;
    exit(0);
}
edd at max:~/svn/rinside/pkg/inst/examples/standard$ ./nandan2; cat nandan2.cpp
value: 1
value: 2
value: 3
value: 4
value: 5
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-

#include <RInside.h>

int main(int argc, char *argv[]) {

    RInside R(argc, argv);
    float sum[5];//int sum[50];
    R.parseEvalQ("arr <- c(1:50)") ;
    for(int i=1;i<=5;i++) {
        char txt[32];
        sprintf(txt, "z <- arr[%d]", i);
        R.parseEvalQ(txt);
        sum[i-1] = R["z"];
    }
    for(int i=0;i<5;i++)
        std::cout << "value: " << sum[i] << std::endl ;
    exit(0);
}
edd at max:~/svn/rinside/pkg/inst/examples/standard$ 



-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list