Greetings Rcpp-devs,
In <Rcpp/vector/Vector.h>, I found two constructors (line 91 and 95, Rcpp version 0.11.2)
Vector( const std::string& st )
Vector( const char* st )
It seems that these are used to parse a string and convert it into a vector (of length 1, since the arguement is a single string, not a vector), possibly of numeric type, similar to as.character("123").
However, when the vector is of numeric type, these constructors will result in an error. Here is the code to reproduce the problem:
> cppFunction('
+ SEXP test()
+ {
+ return NumericVector("123");
+ }
+ ')
> test()
Error: not compatible with requested type
Tracing back the error, I found that the constructors here called
internal::vector_from_string<REALSXP>(st) (<Rcpp/vector/vector_from_string.h>, line 28), which called
r_cast<REALSXP>(str) (<Rcpp/r_cast.h>, line 138), which called
r_true_cast<REALSXP>(x) (<Rcpp/r_cast.h>, line 69), which called
basic_cast<REALSXP>(x) (<Rcpp/r_cast.h>, line 49).
In the last function, x is of type STRSXP, so it will reach the default section of the switch statement, which just throws an error of the message shown in the example.
In fact, the R API function Rf_coerceVector(X, RTYPE) does support the conversion from string to numeric, so it seems reasonable to add STRSXP support.
Greetings Rcpp-devs,
In
<Rcpp/vector/Vector.h>, I found two constructors (line 91 and 95, Rcpp version 0.11.2)It seems that these are used to parse a string and convert it into a vector (of length 1, since the arguement is a single string, not a vector), possibly of numeric type, similar to
as.character("123").However, when the vector is of numeric type, these constructors will result in an error. Here is the code to reproduce the problem:
Tracing back the error, I found that the constructors here called
internal::vector_from_string<REALSXP>(st)(<Rcpp/vector/vector_from_string.h>, line 28), which calledr_cast<REALSXP>(str)(<Rcpp/r_cast.h>, line 138), which calledr_true_cast<REALSXP>(x)(<Rcpp/r_cast.h>, line 69), which calledbasic_cast<REALSXP>(x)(<Rcpp/r_cast.h>, line 49).In the last function,
xis of typeSTRSXP, so it will reach the default section of the switch statement, which just throws an error of the message shown in the example.In fact, the R API function
Rf_coerceVector(X, RTYPE)does support the conversion from string to numeric, so it seems reasonable to addSTRSXPsupport.