@@ -2016,16 +2016,18 @@ namespace attributes {
2016
2016
2017
2017
std::string args = cppArg.substr (createLoc + create.length ());
2018
2018
if (type == " CharacterVector" )
2019
- return " character" + args;
2020
- else if (type == " IntegerVector" )
2021
- return " integer" + args;
2022
- else if (type == " NumericVector" )
2023
- return " numeric" + args;
2024
- else
2025
- return std::string ();
2019
+ return " as.character( c" + args + " )" ;
2020
+ if (type == " IntegerVector" )
2021
+ return " as.integer( c" + args + " )" ;
2022
+ if (type == " NumericVector" )
2023
+ return " as.numeric( c" + args + " )" ;
2024
+ if (type == " LogicalVector" )
2025
+ return " as.logical( c" + args + " )" ;
2026
+
2027
+ return std::string ();
2026
2028
}
2027
2029
2028
- // convert a C++ Matrix to an R argument (returns emtpy string
2030
+ // convert a C++ Matrix to an R argument (returns empty string
2029
2031
// if no conversion possible)
2030
2032
std::string cppMatrixArgToRArg (const std::string& cppArg) {
2031
2033
@@ -2041,7 +2043,7 @@ namespace attributes {
2041
2043
return " matrix" + args;
2042
2044
}
2043
2045
2044
- // convert a C++ literal to an R argument (returns emtpy string
2046
+ // convert a C++ literal to an R argument (returns empty string
2045
2047
// if no conversion possible)
2046
2048
std::string cppLiteralArgToRArg (const std::string& cppArg) {
2047
2049
if (cppArg == " true" )
@@ -2050,14 +2052,45 @@ namespace attributes {
2050
2052
return " FALSE" ;
2051
2053
else if (cppArg == " R_NilValue" )
2052
2054
return " NULL" ;
2053
- else if (cppArg == " NA_STRING" || cppArg == " NA_INTEGER" ||
2054
- cppArg == " NA_LOGICAL" || cppArg == " NA_REAL" ) {
2055
- return " NA" ;
2056
- }
2055
+ else if (cppArg == " NA_STRING" )
2056
+ return " NA_character_" ;
2057
+ else if (cppArg == " NA_INTEGER" )
2058
+ return " NA_integer_" ;
2059
+ else if (cppArg == " NA_LOGICAL" )
2060
+ return " NA_integer_" ;
2061
+ else if (cppArg == " NA_REAL" )
2062
+ return " NA_real_" ;
2057
2063
else
2058
2064
return std::string ();
2059
2065
}
2060
2066
2067
+ // convert an Rcpp container constructor to an R argument
2068
+ // (returns empty string if no conversion possible)
2069
+ std::string cppConstructorArgToRArg (const std::string& cppArg) {
2070
+
2071
+ // map Rcpp containers to R default initializers
2072
+ static std::map<std::string, std::string> RcppContainerToR;
2073
+ RcppContainerToR.insert (std::make_pair (" NumericVector" , " numeric" ));
2074
+ RcppContainerToR.insert (std::make_pair (" DoubleVector" , " numeric" ));
2075
+ RcppContainerToR.insert (std::make_pair (" CharacterVector" , " character" ));
2076
+ RcppContainerToR.insert (std::make_pair (" IntegerVector" , " integer" ));
2077
+ RcppContainerToR.insert (std::make_pair (" LogicalVector" , " logical" ));
2078
+ RcppContainerToR.insert (std::make_pair (" ComplexVector" , " complex" ));
2079
+
2080
+ // for each entry in the map above, see if we find it; if we do,
2081
+ // return the R version
2082
+ typedef std::map<std::string, std::string>::const_iterator Iterator;
2083
+ for (Iterator it = RcppContainerToR.begin (); it != RcppContainerToR.end (); ++it) {
2084
+ size_t loc = cppArg.find (it->first );
2085
+ if (loc != std::string::npos) {
2086
+ return it->second + cppArg.substr (it->first .size (), std::string::npos);
2087
+ }
2088
+ }
2089
+
2090
+ return std::string ();
2091
+
2092
+ }
2093
+
2061
2094
// convert a C++ argument value to an R argument value (returns empty
2062
2095
// string if no conversion is possible)
2063
2096
std::string cppArgToRArg (const std::string& type,
@@ -2087,6 +2120,11 @@ namespace attributes {
2087
2120
if (!rArg.empty ())
2088
2121
return rArg;
2089
2122
2123
+ // try for a constructor arg
2124
+ rArg = cppConstructorArgToRArg (cppArg);
2125
+ if (!rArg.empty ())
2126
+ return rArg;
2127
+
2090
2128
// couldn't parse the arg
2091
2129
return std::string ();
2092
2130
}
0 commit comments