[Rcpp-commits] r3732 - in pkg/RcppExamples: . R man src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 10 03:09:58 CEST 2012


Author: edd
Date: 2012-08-10 03:09:56 +0200 (Fri, 10 Aug 2012)
New Revision: 3732

Added:
   pkg/RcppExamples/R/RcppRNGs.R
   pkg/RcppExamples/man/RcppRNGs.Rd
   pkg/RcppExamples/src/RcppRNGs.cpp
Modified:
   pkg/RcppExamples/ChangeLog
   pkg/RcppExamples/DESCRIPTION
   pkg/RcppExamples/NAMESPACE
   pkg/RcppExamples/NEWS
Log:
new example of Rcpp sugar use for RNG draws
changed maintainer to single person
Version to 0.1.4


Modified: pkg/RcppExamples/ChangeLog
===================================================================
--- pkg/RcppExamples/ChangeLog	2012-08-08 18:32:20 UTC (rev 3731)
+++ pkg/RcppExamples/ChangeLog	2012-08-10 01:09:56 UTC (rev 3732)
@@ -1,3 +1,11 @@
+2012-08-09  Dirk Eddelbuettel  <edd at dexter>
+
+	* src/RcppRNGs.cpp: New example of generating RNG draws
+	* R/RcppRNGs.R: New R function to call new example
+	* man/RcppRNGs.Rd: New manual page for new example
+
+	* DESCRIPTION: Changed Maintainer: to single person per CRAN Policy
+
 2011-12-28  Dirk Eddelbuettel  <edd at debian.org>
 
  	* DESCRIPTION: Release 0.1.3

Modified: pkg/RcppExamples/DESCRIPTION
===================================================================
--- pkg/RcppExamples/DESCRIPTION	2012-08-08 18:32:20 UTC (rev 3731)
+++ pkg/RcppExamples/DESCRIPTION	2012-08-10 01:09:56 UTC (rev 3732)
@@ -1,10 +1,10 @@
 Package: RcppExamples
 Title: Examples using Rcpp to interface R and C++ 
-Version: 0.1.3
+Version: 0.1.4
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, based on code written 
  during 2005 and 2006 by Dominick Samperi 
-Maintainer: Dirk Eddelbuettel and Romain Francois <RomainAndDirk at r-enthusiasts.com>
+Maintainer: Dirk Eddelbuettel <edd at debian.org>
 Description: Examples for Seamless R and C++ integration
  The Rcpp package contains a C++ library that facilitates the integration of
  R and C++ in various ways. This package provides some usage examples.

Modified: pkg/RcppExamples/NAMESPACE
===================================================================
--- pkg/RcppExamples/NAMESPACE	2012-08-08 18:32:20 UTC (rev 3731)
+++ pkg/RcppExamples/NAMESPACE	2012-08-10 01:09:56 UTC (rev 3732)
@@ -7,6 +7,7 @@
        RcppParamsExample,
        RcppVectorExample,
        RcppMatrixExample,
+       RcppRNGs,
        RcppStringVectorExample
 )
 

Modified: pkg/RcppExamples/NEWS
===================================================================
--- pkg/RcppExamples/NEWS	2012-08-08 18:32:20 UTC (rev 3731)
+++ pkg/RcppExamples/NEWS	2012-08-10 01:09:56 UTC (rev 3732)
@@ -1,3 +1,7 @@
+0.1.4   2012-0809
+
+    o	Added new example for Rcpp sugar and vectorised draws of RNGs
+
 0.1.3   2011-12-28
 
     o	Added new example for Rcpp::DataFrame

Added: pkg/RcppExamples/R/RcppRNGs.R
===================================================================
--- pkg/RcppExamples/R/RcppRNGs.R	                        (rev 0)
+++ pkg/RcppExamples/R/RcppRNGs.R	2012-08-10 01:09:56 UTC (rev 3732)
@@ -0,0 +1,27 @@
+## RcppRNGs.R: Rcpp R/C++ interface class library RNGs example
+##
+## Copyright (C) 2012        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/>.
+
+RcppRNGs <- function(n) {
+
+    ## Make the call...
+    df <- .Call("RcppRNGs", n, PACKAGE="RcppExamples")
+
+    df
+}
+

Added: pkg/RcppExamples/man/RcppRNGs.Rd
===================================================================
--- pkg/RcppExamples/man/RcppRNGs.Rd	                        (rev 0)
+++ pkg/RcppExamples/man/RcppRNGs.Rd	2012-08-10 01:09:56 UTC (rev 3732)
@@ -0,0 +1,47 @@
+\name{RcppRNGs}
+\alias{RcppRNGs}
+\title{Rcpp RNGs example}
+\description{
+  Rcpp sugar provides numerous p/q/d/r functions for numerous distributions.
+
+  This example shows (in the corresponding C++ code) how to draw from
+  three different distributions and returns a data frame.
+}
+\details{
+  The various header file, and the Rcpp sugar vignette, provide full
+  documentation for Rcpp sugar.
+
+  The C++ source file corresponding to the this function does the
+  following (inside of a \code{try/catch} block):
+
+  \preformatted{%
+        Rcpp::RNGScope scope;         		// needed when RNGs are drawn
+
+        int n = Rcpp::as<int>(ns); 		// length vector 
+        Rcpp::NumericVector rn = Rcpp::rnorm(n);
+        Rcpp::NumericVector rt = Rcpp::rt(n, 1.0);
+        Rcpp::NumericVector rp = Rcpp::rpois(n, 1.0);
+
+        // create a new data frame to return drawns
+        Rcpp::DataFrame NDF = 
+            Rcpp::DataFrame::create(Rcpp::Named("rnorm") =rn,
+                                    Rcpp::Named("rt")    =rt,
+                                    Rcpp::Named("rpois") =rp);
+
+        // and return old and new in list
+        return(NDF);
+  }
+
+  As shown in the example section, provided the seed is reset, the exact
+  same draws can be obtained in R itself -- which is important for reproducibility.
+}
+\author{Dirk Eddelbuettel and Romain Francois}
+\examples{
+  set.seed(42)
+  X <- RcppRNGs(10)
+  set.seed(42)
+  Y <- data.frame(rnorm=rnorm(10),rt=rt(10,1),rpois=rpois(10,1))
+  all.equal(X,Y)
+}
+\keyword{programming}
+\keyword{interface}

Added: pkg/RcppExamples/src/RcppRNGs.cpp
===================================================================
--- pkg/RcppExamples/src/RcppRNGs.cpp	                        (rev 0)
+++ pkg/RcppExamples/src/RcppRNGs.cpp	2012-08-10 01:09:56 UTC (rev 3732)
@@ -0,0 +1,53 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// RcppRNGs.cpp: Rcpp R/C++ interface class library RNG example
+//
+// Copyright (C) 2012        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 RcppRNGs(SEXP ns) {
+
+    try {					// or use BEGIN_RCPP macro
+        
+        Rcpp::RNGScope scope;         		// needed when RNGs are drawn
+
+        int n = Rcpp::as<int>(ns); 		// length vector 
+        Rcpp::NumericVector rn = Rcpp::rnorm(n);
+        Rcpp::NumericVector rt = Rcpp::rt(n, 1.0);
+        Rcpp::NumericVector rp = Rcpp::rpois(n, 1.0);
+
+        // create a new data frame to return drawns
+        Rcpp::DataFrame NDF = 
+            Rcpp::DataFrame::create(Rcpp::Named("rnorm") =rn,
+                                    Rcpp::Named("rt")    =rt,
+                                    Rcpp::Named("rpois") =rp);
+
+        // and return old and new in list
+        return(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