A continuation of this question: In Rcpp, how to get a user-defined structure from C into R
How do you get the Rcpp template to return strings and numbers, rather than some type of "Vectors"? I am attempting to port someone else's C code into R and they use all kinds of different data types (char,unsigned char,short,unsigned short,int,float,long int,long unsigned int,double - no, really, all of these are in the actual header!) that I need to "coerce" into strings and numbers in R. Seeing the comment from "Ralf Stubner", I modified your example to generate a MWE showing the problem:
#include <RcppCommon.h>
typedef struct {
char firstname[128];
// long unsigned int big_number;
} HEADER_INFO;
namespace Rcpp {
template <>
SEXP wrap(const HEADER_INFO& x);
}
#include <Rcpp.h>
namespace Rcpp {
template <>
SEXP wrap(const HEADER_INFO& x) {
Rcpp::CharacterVector firstname(x.firstname, x.firstname + 128);
// Rcpp::Integer big_number(x.big_number);
return Rcpp::wrap(Rcpp::List::create(Rcpp::Named("firstname") = firstname
// ,Rcpp::Named("big_number") = big_number
));
};
}
// [[Rcpp::export]]
HEADER_INFO getHeaderInfo() {
HEADER_INFO header;
strcpy( header.firstname, "Albert" );
// header.big_number = 123456789012345;
return header;
}
/*** R
getHeaderInfo()
*/
When you run this in R:
> getHeaderInfo()
$firstname
[1] "65" "108" "98" "101" "114" "116" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[19] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[37] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[55] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[73] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[91] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0"
[109] "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "127" "16"
[127] "72" "-29"
The unsigned long entries are commented out because I could not find a datatype in the Rcpp Namespace that would compile. Is it true that types such as "Rcpp::Integer" and "Rcpp::Double" don't exist? If I have to use IntegerVector, then how do I tell the template that I want to refer to the result in R as just an integer and not as vector of length 1?