[Rcpp-commits] r2975 - in pkg/RcppExamples: . R man src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Apr 8 16:31:35 CEST 2011
Author: edd
Date: 2011-04-08 16:31:35 +0200 (Fri, 08 Apr 2011)
New Revision: 2975
Added:
pkg/RcppExamples/R/RcppDataFrame.R
pkg/RcppExamples/man/RcppDataFrame.Rd
pkg/RcppExamples/src/RcppDataFrame.cpp
Modified:
pkg/RcppExamples/ChangeLog
pkg/RcppExamples/NAMESPACE
Log:
added simple new example for Rcpp::DataFrame
Modified: pkg/RcppExamples/ChangeLog
===================================================================
--- pkg/RcppExamples/ChangeLog 2011-04-07 20:24:29 UTC (rev 2974)
+++ pkg/RcppExamples/ChangeLog 2011-04-08 14:31:35 UTC (rev 2975)
@@ -1,3 +1,9 @@
+2011-04-08 Dirk Eddelbuettel <edd at debian.org>
+
+ * R/RcppDataFrame.R: Added new example for Rcpp::DataFrame
+ * src/RcppDataFrame.cpp: C++ source for new example
+ * man/RcppDataFrame.Rd: Documentation
+
2010-12-20 Dirk Eddelbuettel <edd at debian.org>
* DESCRIPTION: Release 0.1.2
Modified: pkg/RcppExamples/NAMESPACE
===================================================================
--- pkg/RcppExamples/NAMESPACE 2011-04-07 20:24:29 UTC (rev 2974)
+++ pkg/RcppExamples/NAMESPACE 2011-04-08 14:31:35 UTC (rev 2975)
@@ -2,6 +2,7 @@
export(RcppExample,
print.RcppExample,
+ RcppDataFrame,
RcppDateExample,
RcppParamsExample,
RcppVectorExample,
Added: pkg/RcppExamples/R/RcppDataFrame.R
===================================================================
--- pkg/RcppExamples/R/RcppDataFrame.R (rev 0)
+++ pkg/RcppExamples/R/RcppDataFrame.R 2011-04-08 14:31:35 UTC (rev 2975)
@@ -0,0 +1,39 @@
+
+## RcppDataFrame.R: Rcpp R/C++ interface class library DataFrame example
+##
+## Copyright (C) 2011 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/>.
+
+RcppDataFrame <- function() {
+
+ ## create a simple data.frame
+ ## here we enforce strings, factors can be used too
+ D <- data.frame(a=1:3,
+ b=LETTERS[1:3],
+ c=as.Date("2011-01-01")+0:2,
+ stringsAsFactors=FALSE)
+ cat("Original data frame before call:\n")
+ print(D)
+
+ ## Make the call...
+ val <- .Call("RcppDataFrame", D, PACKAGE="RcppExamples")
+
+ cat("\nAfter call, original and new data frames:\n")
+ print(val)
+
+ val
+}
Added: pkg/RcppExamples/man/RcppDataFrame.Rd
===================================================================
--- pkg/RcppExamples/man/RcppDataFrame.Rd (rev 0)
+++ pkg/RcppExamples/man/RcppDataFrame.Rd 2011-04-08 14:31:35 UTC (rev 2975)
@@ -0,0 +1,22 @@
+\name{RcppDataFrame}
+\alias{RcppDataFrame}
+\title{Rcpp::DataFrame example for Rcpp}
+\description{
+ A \code{DataFrame} can be passed C++ and can be instantiated as a
+ corresponding C++ object using the Rcpp API.
+
+ This example shows (in the corresponding C++ code) how to access,
+ modify and create a data frame.
+}
+\details{
+ Usage of \code{Rcpp::DataFrame} is fully defined in
+ the respective header file.
+}
+\author{Dirk Eddelbuettel and Romain Francois}
+\examples{
+ \dontrun{
+ RcppDataFrame()
+ }
+}
+\keyword{programming}
+\keyword{interface}
Added: pkg/RcppExamples/src/RcppDataFrame.cpp
===================================================================
--- pkg/RcppExamples/src/RcppDataFrame.cpp (rev 0)
+++ pkg/RcppExamples/src/RcppDataFrame.cpp 2011-04-08 14:31:35 UTC (rev 2975)
@@ -0,0 +1,60 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// DataFrame.cpp: Rcpp R/C++ interface class library data frame example
+//
+// Copyright (C) 2011 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 RcppDataFrame(SEXP Dsexp) {
+
+ try { // or use BEGIN_RCPP macro
+
+ // construct the data.frame object
+ Rcpp::DataFrame DF = Rcpp::DataFrame(Dsexp);
+
+ // and access each column by name
+ Rcpp::IntegerVector a = DF["a"];
+ Rcpp::CharacterVector b = DF["b"];
+ Rcpp::DateVector c = DF["c"];
+
+ // do something
+ a[2] = 42;
+ b[1] = "foo";
+ c[0] = c[0] + 7; // move up a week
+
+ // create a new data frame
+ Rcpp::DataFrame NDF =
+ Rcpp::DataFrame::create(Rcpp::Named("a")=a,
+ Rcpp::Named("b")=b,
+ Rcpp::Named("c")=c);
+
+ // and return old and new in list
+ return(Rcpp::List::create(Rcpp::Named("origDataFrame")=DF,
+ Rcpp::Named("newDataFrame")=NDF));
+
+ } catch( std::exception &ex ) { // or use END_RCPP macro
+ forward_exception_to_r( ex );
+ } catch(...) {
+ ::Rf_error( "c++ exception (unknown reason)" );
+ }
+ return R_NilValue; // -Wall
+}
+
+
+
More information about the Rcpp-commits
mailing list