[Rcpp-commits] r202 - in pkg: inst/examples/RcppInline src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Dec 19 19:54:49 CET 2009
Author: edd
Date: 2009-12-19 19:54:49 +0100 (Sat, 19 Dec 2009)
New Revision: 202
Modified:
pkg/inst/examples/RcppInline/RcppSexpTests.r
pkg/src/RcppSexp.cpp
pkg/src/RcppSexp.h
Log:
complete example -- RcppSexp now deals elegantly and easily with double, int and std::string scalars and vectors
Modified: pkg/inst/examples/RcppInline/RcppSexpTests.r
===================================================================
--- pkg/inst/examples/RcppInline/RcppSexpTests.r 2009-12-19 18:39:34 UTC (rev 201)
+++ pkg/inst/examples/RcppInline/RcppSexpTests.r 2009-12-19 18:54:49 UTC (rev 202)
@@ -55,38 +55,33 @@
for (size_t i=0; i<iv.size(); i++) {
iv[i] = 2*iv[i];
}
- RcppSexp t = RcppSexp( iv );
- return(t.asSexp());
+ return(RcppSexp( iv ).asSexp());
'
funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
print(funx(x=2:5))
-cat("\n===Double Vector via RcppResultSet.getSEXP\n")
+cat("\n===Double Vector\n")
foo <- '
std::vector<double> iv = RcppSexp(x).asStdVectorDouble();
std::cout << "Returning twice the value of vector : ";
for (size_t i=0; i<iv.size(); i++) {
iv[i] = 2*iv[i];
}
- RcppResultSet rs;
- rs.add("", iv);
- return(rs.getSEXP());
+ return(RcppSexp( iv ).asSexp());
'
funx <- cfunction(signature(x="numeric"), foo, Rcpp=TRUE, verbose=FALSE)
print(funx(x=0.1+2:5))
-cat("\n===String Vector via RcppResultSet.getSEXP\n")
+cat("\n===String Vector\n")
foo <- '
std::vector<std::string> iv = RcppSexp(x).asStdVectorString();
std::cout << "Returning twice the value of vector : ";
for (size_t i=0; i<iv.size(); i++) {
iv[i] = iv[i] + iv[i];
}
- RcppResultSet rs;
- rs.add("", iv);
- return(rs.getSEXP());
+ return(RcppSexp( iv ).asSexp());
'
funx <- cfunction(signature(x="character"), foo, Rcpp=TRUE, verbose=FALSE)
print(funx(x=c("foo", "bar")))
Modified: pkg/src/RcppSexp.cpp
===================================================================
--- pkg/src/RcppSexp.cpp 2009-12-19 18:39:34 UTC (rev 201)
+++ pkg/src/RcppSexp.cpp 2009-12-19 18:54:49 UTC (rev 202)
@@ -52,6 +52,26 @@
}
}
+RcppSexp::RcppSexp(const std::vector<double> & v) {
+ logTxt("RcppSexp from double vector\n");
+ int n = v.size();
+ m_sexp = Rf_allocVector(REALSXP, n);
+ R_PreserveObject(m_sexp);
+ for (int i = 0; i < n; i++) {
+ REAL(m_sexp)[i] = v[i];
+ }
+}
+
+RcppSexp::RcppSexp(const std::vector<std::string> & v) {
+ logTxt("RcppSexp from std::string vector\n");
+ int n = v.size();
+ m_sexp = Rf_allocVector(STRSXP, n);
+ R_PreserveObject(m_sexp);
+ for (int i = 0; i < n; i++) {
+ SET_STRING_ELT(m_sexp, i, Rf_mkChar(v[i].c_str()));
+ }
+}
+
RcppSexp::~RcppSexp() {
logTxt("dtor");
R_ReleaseObject(m_sexp);
Modified: pkg/src/RcppSexp.h
===================================================================
--- pkg/src/RcppSexp.h 2009-12-19 18:39:34 UTC (rev 201)
+++ pkg/src/RcppSexp.h 2009-12-19 18:54:49 UTC (rev 202)
@@ -32,6 +32,8 @@
RcppSexp(const int & v);
RcppSexp(const std::string & v);
RcppSexp(const std::vector<int> & v);
+ RcppSexp(const std::vector<double> & v);
+ RcppSexp(const std::vector<std::string> & v);
~RcppSexp();
double asDouble() const;
More information about the Rcpp-commits
mailing list