[Rcpp-commits] r836 - in pkg/RcppExamples: . R inst man src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 5 03:27:23 CET 2010
Author: edd
Date: 2010-03-05 03:27:22 +0100 (Fri, 05 Mar 2010)
New Revision: 836
Added:
pkg/RcppExamples/R/RcppStringVectorExample.R
pkg/RcppExamples/src/RcppStringVectorExample.cpp
Modified:
pkg/RcppExamples/NAMESPACE
pkg/RcppExamples/inst/ChangeLog
pkg/RcppExamples/man/RcppVector.Rd
pkg/RcppExamples/src/RcppMatrixExample.cpp
Log:
added RcppStringVectorExample
Modified: pkg/RcppExamples/NAMESPACE
===================================================================
--- pkg/RcppExamples/NAMESPACE 2010-03-04 21:14:36 UTC (rev 835)
+++ pkg/RcppExamples/NAMESPACE 2010-03-05 02:27:22 UTC (rev 836)
@@ -5,7 +5,8 @@
RcppDateExample,
RcppParamsExample,
RcppVectorExample,
- RcppMatrixExample
+ RcppMatrixExample,
+ RcppStringVectorExample
)
#importFrom( utils, capture.output )
Added: pkg/RcppExamples/R/RcppStringVectorExample.R
===================================================================
--- pkg/RcppExamples/R/RcppStringVectorExample.R (rev 0)
+++ pkg/RcppExamples/R/RcppStringVectorExample.R 2010-03-05 02:27:22 UTC (rev 836)
@@ -0,0 +1,34 @@
+
+## RcppStringVectorExample.R: Rcpp R/C++ interface class library
+##
+## Copyright (C) 2008 Dirk Eddelbuettel
+## Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois
+##
+## This file is part of Rcpp.
+##
+## Rcpp is free software: you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation, either version 2 of the License, or
+## (at your option) any later version.
+##
+## Rcpp is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+RcppStringVectorExample <- function(vec=c("Tick", "Tack", "Tock"),
+ api=c("classic", "new")) {
+
+ api <- match.arg(api) # match to classic or new
+ fun <- paste(api, "RcppStringVectorExample", sep="")
+
+ ## Make the call...
+ val <- .Call(fun, # either new or classic
+ vec,
+ PACKAGE="RcppExamples")
+
+ val
+}
Modified: pkg/RcppExamples/inst/ChangeLog
===================================================================
--- pkg/RcppExamples/inst/ChangeLog 2010-03-04 21:14:36 UTC (rev 835)
+++ pkg/RcppExamples/inst/ChangeLog 2010-03-05 02:27:22 UTC (rev 836)
@@ -1,7 +1,9 @@
2010-03-04 Dirk Eddelbuettel <edd at dexter>
* src/RcppMatrixExample.cpp: Added 'classic' + 'new' API examples
- * R/RcppVectorExample.R: idem
+ * R/RcppMatrixExample.R: idem
+ * src/RcppStringVectorExample.cpp: idem
+ * R/RcppStringVectorExample.R: idem
2010-03-03 Dirk Eddelbuettel <edd at dexter>
Modified: pkg/RcppExamples/man/RcppVector.Rd
===================================================================
--- pkg/RcppExamples/man/RcppVector.Rd 2010-03-04 21:14:36 UTC (rev 835)
+++ pkg/RcppExamples/man/RcppVector.Rd 2010-03-05 02:27:22 UTC (rev 836)
@@ -5,6 +5,8 @@
\alias{RcppMatrixView}
\alias{RcppStringVector}
\alias{RcppVectorExample}
+\alias{RcppMatrixExample}
+\alias{RcppStringVectorExample}
\title{C++ classes for receiving R object in C++}
\description{
\code{RcppVector}, \code{RcppMatrix} and \code{RcppStringVector} are
Modified: pkg/RcppExamples/src/RcppMatrixExample.cpp
===================================================================
--- pkg/RcppExamples/src/RcppMatrixExample.cpp 2010-03-04 21:14:36 UTC (rev 835)
+++ pkg/RcppExamples/src/RcppMatrixExample.cpp 2010-03-05 02:27:22 UTC (rev 836)
@@ -26,7 +26,7 @@
RcppExport SEXP newRcppMatrixExample(SEXP matrix) {
Rcpp::NumericMatrix orig(matrix); // creates Rcpp matrix from SEXP
- Rcpp::NumericMatrix mat(orig.nrow(), orig.ncol()); // keep a copy
+ Rcpp::NumericMatrix mat(orig.nrow(), orig.ncol());
// we could query size via
// int n = mat.nrow(), k=mat.ncol();
Added: pkg/RcppExamples/src/RcppStringVectorExample.cpp
===================================================================
--- pkg/RcppExamples/src/RcppStringVectorExample.cpp (rev 0)
+++ pkg/RcppExamples/src/RcppStringVectorExample.cpp 2010-03-05 02:27:22 UTC (rev 836)
@@ -0,0 +1,78 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppStringVectorExample.cpp: Rcpp R/C++ interface class library
+//
+// Copyright (C) 2005 - 2006 Dominick Samperi
+// Copyright (C) 2008 Dirk Eddelbuettel
+// Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp.h>
+
+RcppExport SEXP newRcppStringVectorExample(SEXP strvec) {
+
+ Rcpp::StringVector orig(strvec); // creates Rcpp string vector from SEXP
+ Rcpp::StringVector vec(orig.size());
+
+ for (int i=0; i<orig.size(); i++) {
+ std::string txt(orig[i]);
+ std::transform(txt.begin(), txt.end(), txt.begin(), ::tolower);
+ vec[i] = txt;
+ }
+
+ Rcpp::Pairlist res(Rcpp::Named( "result", vec),
+ Rcpp::Named( "original", orig));
+
+ return res;
+}
+
+RcppExport SEXP classicRcppStringVectorExample(SEXP strvec) {
+
+ SEXP rl = R_NilValue; // Use this when there is nothing to be returned.
+ char *exceptionMesg = NULL;
+
+ try {
+
+ RcppStringVector orig(strvec);
+ RcppStringVector vec(strvec);
+
+ for (int i=0; i<orig.size(); i++) {
+ std::transform(orig(i).begin(), orig(i).end(),
+ vec(i).begin(), ::tolower);
+ }
+ // Build result set to be returned as a list to R.
+ RcppResultSet rs;
+
+ rs.add("result", vec);
+ rs.add("original", orig);
+
+ // Get the list to be returned to R.
+ rl = rs.getReturnList();
+
+ } catch(std::exception& ex) {
+ exceptionMesg = copyMessageToR(ex.what());
+ } catch(...) {
+ exceptionMesg = copyMessageToR("unknown reason");
+ }
+
+ if(exceptionMesg != NULL)
+ Rf_error(exceptionMesg);
+
+ return rl;
+}
+
+
More information about the Rcpp-commits
mailing list