[Rcpp-commits] r697 - in pkg: Rcpp Rcpp/man Rcpp/src RcppExamples/man RcppExamples/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Feb 16 17:21:28 CET 2010
Author: edd
Date: 2010-02-16 17:21:27 +0100 (Tue, 16 Feb 2010)
New Revision: 697
Added:
pkg/RcppExamples/man/RcppDate.Rd
pkg/RcppExamples/man/RcppExample.Rd
pkg/RcppExamples/man/RcppParams.Rd
pkg/RcppExamples/man/RcppResultSet.Rd
pkg/RcppExamples/man/RcppVector.Rd
pkg/RcppExamples/src/RcppExample.cpp
Removed:
pkg/Rcpp/man/RcppDate.Rd
pkg/Rcpp/man/RcppExample.Rd
pkg/Rcpp/man/RcppParams.Rd
pkg/Rcpp/man/RcppResultSet.Rd
pkg/Rcpp/man/RcppVector.Rd
pkg/Rcpp/src/RcppExample.cpp
Modified:
pkg/Rcpp/NAMESPACE
Log:
moved manual pages
Modified: pkg/Rcpp/NAMESPACE
===================================================================
--- pkg/Rcpp/NAMESPACE 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/NAMESPACE 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,12 +1,6 @@
useDynLib(Rcpp)
-export(RcppExample,
- print.RcppExample,
- RcppDateExample,
- RcppParamsExample,
- RcppVectorExample,
- Rcpp.package.skeleton
-)
+export(Rcpp.package.skeleton)
importFrom( utils, capture.output )
Deleted: pkg/Rcpp/man/RcppDate.Rd
===================================================================
--- pkg/Rcpp/man/RcppDate.Rd 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/man/RcppDate.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,91 +0,0 @@
-\name{RcppDate}
-\alias{RcppDate}
-\alias{RcppDatetime}
-\alias{RcppDateVector}
-\alias{RcppDatetimeVector}
-\alias{RcppDateExample}
-\title{C++ classes for receiving date and datetime R objects in C++}
-\description{
- \code{RcppDate}, \code{RcppDatetime}, \code{RcppDateVector} and
- \code{RcppDatetimeVector} are C++ classes defined in \code{Rcpp.h} that can
- pass scalars and vectors of of \R objects of types \code{Date} and
- \code{POSIXct}, respectively, to C++ via the \code{.Call()} function interface.
-
- Member functions are provided to query the dimension of the vector or
- matrix object, convert it in a corresponding \code{C} representation.
-
- \R objects of type \code{Date}, and hence the \code{RcppDate} and
- \code{RcppDateVector} objects, are internally represented as an integer
- counting days since the epoch, i.e. January 1, 1970. Similarly, \R objects of type
- \code{POSIXct} and the \code{RcppDatetime} and
- \code{RcppDatetimeVector} objects, are internally represented as
- seconds since the epoch. However, \R extends the POSIX standard by
- using a double leading to microsecond precision in timestamps. This is
- fully supported by \code{Rcpp} as well.
-}
-%\usage{
-%}
-%\arguments{
-%}
-%\value{
-% Internal to the C++ code.
-%}
-\details{
- Usage of the \code{RcppDate}, \code{RcppDatetime} (and their vector
- extensions) in \code{C++} is fully defined in \code{Rcpp.h}.
-
- As example, consider a call from \R to \code{C++} such as
-
- \preformatted{
- # an R example passing one type of each class to a function
- # someFunction in package somePackage
- val <- .Call("someFunction",
- Sys.Date(), # current date
- Sys.time(), # current timestamp
- as.Date("2000-02-25")
- + 0:5, # date vector
- ISOdatetime(1999,12,31,23,59,0)
- + (0:5)*0.250, # datetime vector
- PACKAGE="somePackage")
- }
-
- At the \code{C++} level, the corresponding code to assign these parameter to
- \code{C++} objects is can be as follows::
- \preformatted{%
- SEXP someFunction(SEXP ds, SEXP dts,
- SEXP dvs, SEXP dtvs) {
-
- RcppDate d(ds);
- RcppDatetime dt(dts);
- RcppDateVector dv(dvs);
- RcppDatetimeVector dtv(dtvs);
- }
- }
-
- Standard accessor functions are defined, see \code{Rcpp.h} for details.
-
- Objects of these types can also be returned via \code{RcppResultSet}.
-
-}
-%\references{
-% See \code{Rcpp.h} and the package vignette.
-%}
-\seealso{
- \code{RcppResultSet}, the vignette \dQuote{RcppAPI}.
-}
-\author{Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk
- Eddelbuettel made some additions, and became maintainer in 2008.}
-\examples{
-
-# set up date and datetime vectors
-dvec <- Sys.Date() + -2:2
-dtvec <- Sys.time() + (-2:2)*0.5
-
-# call the underlying C++ function
-result <- RcppDateExample(dvec, dtvec)
-
-# inspect returned object
-result
-}
-\keyword{programming}
-\keyword{interface}
Deleted: pkg/Rcpp/man/RcppExample.Rd
===================================================================
--- pkg/Rcpp/man/RcppExample.Rd 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/man/RcppExample.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,112 +0,0 @@
-\name{RcppExample}
-\alias{RcppExample}
-\alias{print.RcppExample}
-\title{Rcpp R / C++ interface example}
-\description{
- \code{RcppExample} illustrates how the \code{Rcpp} R/C++
- interface class library is used.
-}
-\usage{
-RcppExample(params, nlist, numvec, nummat, df, datevec, stringvec,
-fnvec, fnlist)
-\method{print}{RcppExample}(x,...)
-}
-\arguments{
- \item{params}{A heterogeneous list specifying \code{method} (string),
- \code{tolerance} (double), \code{maxIter} (int).}
- \item{nlist}{a list of named numeric values (double or int).}
- \item{numvec}{a numeric 1D vector (double or int).}
- \item{nummat}{a numeric 2D matrix (double or int).}
- \item{df}{a data frame.}
- \item{datevec}{a vector of Date's.}
- \item{stringvec}{a vector of strings.}
- \item{fnvec}{an R function with numeric vector argument.}
- \item{fnlist}{an R function with list argument.}
- \item{x}{Object of type \code{RcppExample}.}
- \item{...}{Extra named parameters.}
- }
-\details{
-
- The C++ represention of data frames are not passed back to R in a form
- that R recognizes as a data frame, but it is a simple matter to do the
- conversion. For example, the return value named \code{PreDF} (see
- return values below) is not seen as a data frame on the R side (thus
- the name "pre-data frame"), but it can be converted to a data frame
- using \code{df <- data.frame(result$PreDF)}.
-
- The print.RcppExample() function is defined so that we can control
- what gets printed when a variable assigned the return value is entered
- on a line by itself. It is defined to simply list the names of the
- fields returned (see RcppExample.R).
-
- The source files for this
- example and for the \code{Rcpp} class library
- can be found in the RcppTemplate package source archive (the .tar.gz file).
-}
-\value{
- \code{RcppExample} returns a list containing:
- \item{method}{string input paramter}
- \item{tolerance}{double input paramter}
- \item{maxIter}{int input parameter}
- \item{nlFirstName}{first name in nlist}
- \item{nlFirstValue}{first value in nlist}
- \item{matD}{R matrix from an RcppMatrix<double> object}
- \item{stlvec}{R vector from a vector<double> object}
- \item{stlmat}{R matrix from a vector<vector<double> > object}
- \item{a}{R matrix from C/C++ matrix}
- \item{v}{R vector from C/C++ vector}
- \item{strings}{R vector of strings from vector<string> object}
- \item{InputDF}{a data frame passed in from R}
- \item{PreDF}{a data frame created on C++ side to be passed back to R}
- \item{params}{input parameter list (this is redundant because we returned the input parameters above)}
- }
-
-\author{Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk
- Eddelbuettel made some additions, and became maintainer in 2008.}
-
-\references{
- Samperi, D., (2006) \emph{Rcpp: R/C++ Interface Classes:
- Using C++ Libraries from R},
- available as a package vignette, or in the \code{RcppTemplate}
- package \code{doc} subdirectory
- (\code{RcppAPI.pdf}).
-
- \emph{Writing R Extensions}, available at \url{http:www.r-project.org}.
-}
-\examples{
-
-params <- list(method='BFGS',
- tolerance=1.0e-8,
- maxIter=1000,
- startDate=as.Date('2006-7-15'))
-
-nlist <- list(ibm = 80.50, hp = 53.64, c = 45.41)
-
-numvec <- seq(1,5) # numerical vector
-
-nummat <- matrix(seq(1,20),4,5) # numerical matrix
-
-stringvec <- c("hello", "world", "fractal") # string vector
-
-datestr <- c('2006-6-10', '2006-7-12', '2006-8-10')
-datevec <- as.Date(datestr, "\%Y-\%m-\%d") # date vector
-
-df <- data.frame(a=c(TRUE, TRUE, FALSE), b=I(c('a','b','c')),
-c=c('beta', 'beta', 'gamma'), dates=datevec)
-
-fnvec <- function(x) { sum(x) } # Add up components of vector
-
-fnlist <- function(l) { # Return vector with 1 added to each component
- vec <- c(l$alpha + 1, l$beta + 1, l$gamma + 1)
- vec
-}
-
-result <- RcppExample(params, nlist, numvec, nummat, df, datevec,
- stringvec, fnvec, fnlist)
-
-result
-
-}
-
-\keyword{programming}
-\keyword{interface}
Deleted: pkg/Rcpp/man/RcppParams.Rd
===================================================================
--- pkg/Rcpp/man/RcppParams.Rd 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/man/RcppParams.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,90 +0,0 @@
-\name{RcppParams}
-\alias{RcppParams}
-\alias{RcppParamsExample}
-\title{C++ class for receiving (scalar) parameters from R}
-\description{
- \code{RcppParams} is a C++ class defined in \code{Rcpp.h} that receive
- any number of scalar parameters of types in a single named list object
- from \R through the \code{.Call()} function.
-
- The parameters can be of different types that are limited to the \R
- types \code{numeric}, \code{integer}, \code{character}, \code{logical}
- or \code{Date}. These types are mapped into, respectively, the
- corresponding C++ types \code{double}, \code{int}, \code{string},
- \code{bool} and \code{Date} (a custom class defined by \code{Rcpp}.
-}
-\usage{
- val <- RcppParamsExample(params)
-}
-\arguments{
- \item{params}{A heterogeneous list specifying \code{method} (string),
- \code{tolerance} (double), \code{maxIter} (int) and \code{startDate}
- (Date in R, RcppDate in C++).}
-}
-\value{
- \code{RcppExample} returns a list containing:
- \item{method}{string input paramter}
- \item{tolerance}{double input paramter}
- \item{maxIter}{int input parameter}
- \item{startDate}{Date type with starting date}
- \item{params}{input parameter list (this is redundant because we
- returned the input parameters above)}
-}
-\details{
- Usage of \code{RcppParams} from \R via \code{.Call()} is as follows:
- \preformatted{%
- # an R example passing one type of each class to a function
- # someFunction in package somePackage
- val <- .Call("someFunction",
- list(pie=3.1415, magicanswer=42, sometext="foo",
- yesno=true, today=Sys.date()),
- PACKAGE="somePackage")
- }
-
- At the C++ level, the corresponding code to assign these parameter to
- C++ objects is
- \preformatted{%
- SEXP someFunction(SEXP params) {
- RcppParams par(params);
- double p = par.getDoubleValue("pie");
- int magic = par.getIntValue("magicanswer");
- string txt = par.getStringValue("sometext");
- bool yn = par.getBoolValue("yesno");
- RcppDate d = par.getDateValue("today");
- // some calculations ...
- // some return values ...
- }
- }
- As the lookup is driven by the names givem at the \R level, order is
- not important. It is however important that the types match. Errors
- are typically caught and an exception is thrown.
-
- The class member function \code{checkNames} can be used to verify that the
- \code{SEXP} object passed to the function contains a given set of
- named object.
-}
-%\references{
-% See \code{Rcpp.h} and the package vignette.
-%}
-\seealso{
- \code{RcppExample}, the vignette \dQuote{RcppAPI}.
-}
-\author{Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk
- Eddelbuettel made some additions, and became maintainer in 2008.}
-\examples{
-
-# set up some value
-params <- list(method='BFGS',
- tolerance=1.0e-5,
- maxIter=100,
- startDate=as.Date('2006-7-15'))
-
-# call the underlying C++ function
-result <- RcppParamsExample(params)
-
-# inspect returned object
-result
-
-}
-\keyword{programming}
-\keyword{interface}
Deleted: pkg/Rcpp/man/RcppResultSet.Rd
===================================================================
--- pkg/Rcpp/man/RcppResultSet.Rd 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/man/RcppResultSet.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,101 +0,0 @@
-\name{RcppResultSet}
-\alias{RcppResultSet}
-\title{C++ class for sending C++ objects back to R}
-\description{
- \code{RcppResultSet} is a C++ class defined in \code{Rcpp.h} that can
- assign any number of C++ objects to \R in a single named list object
- as the \code{SEXP} return value of a \code{.Call()} function call.
-
- The C++ objects can be of different types that are limited to
- types \code{double}, \code{int}, \code{string}, vectors of
- \code{double} or \code{int} (with explicit dimensions),
- matrices of \code{double} or \code{int} (with explicit dimensions),
- STL vectors of \code{double}, \code{int} or \code{string}, STL
- \sQuote{vector of vectors} of types \code{double} or \code{int} (all
- with implicit dimensions), the internal types \code{RcppDate}, \code{RcppDateVector},
- \code{RcppStringVector}, \code{RcppVector} of types \code{double} or
- \code{int}, \code{RcppMatrix} of types \code{double} or \code{int}
- as well \code{RcppFrame}, a type that can be converted into a
- \code{data.frame}, and the \R type \code{SEXP}.
-
- Where applicable, the \code{C++} types are automatically converted to the
- corresponding \R types structures around types \code{numeric},
- \code{integer}, or \code{character}. The \code{C++} code can all be
- retrieved in \R as elements of a named list object.
-
-}
-%\usage{
-%}
-%\arguments{
-%}
-%\value{
-% Internal to the C++ code.
-%}
-\details{
- Usage of \code{RcppResultSet} from \code{C++} is fully defined in
- \code{Rcpp.h}. An example for returning data to \R at the end of a
- \code{.Call()} call follows.
-
- At the C++ level, the corresponding code to assign these parameter to
- C++ objects is can be as follows (taken from the C++ source of
- \code{RcppExample}):
- \preformatted{%
- SEXP rl;
- RcppResultSet rs;
-
- rs.add("date", aDate); // RcppDate
- rs.add("dateVec", dateVec); // RcppDateVec
- rs.add("method", method); // string
- rs.add("tolerance", tol); // numeric
- rs.add("maxIter", maxIter); // int
- rs.add("matD", matD); // RcppMatrix
- rs.add("stlvec", stlvec); // vector<double> or <int>
- rs.add("stlmat", stlmat); // vector< vector <double> >
- // or <int>
- rs.add("a", a, nrows, ncols); // double** (or int**) with
- // two dimension
- rs.add("v", v, len); // double* (or int*) with
- // one dimension
- rs.add("stringVec", strVec); // RcppStringVector
- rs.add("strings", svec); // vector<string>
- rs.add("InputDF", inframe); // RcppFrame
- rs.add("PreDF", frame); // RcppFrame
-
- rl = rs.getReturnList();
- return(rl);
- }
-
- As the \R level, we assign the returned object a list variables from
- which we select each list element by its name.
- lookup is driven by the names givem at the \R level, order is
- not important. It is however important that the types match. Errors
- are typically caught and an exception is thrown.
-
- The class member function \code{checkNames} can be used to verify that the
- \code{SEXP} object passed to the function contains a given set of
- named object.
-
-}
-%\references{
-% See \code{Rcpp.h} and the package vignette.
-%}
-\seealso{
- \code{RcppExample}, the vignette \dQuote{RcppAPI}.
-}
-\author{Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk
- Eddelbuettel made some additions, and became maintainer in 2008.}
-\examples{
-
-# example from RcppDate
-# set up date and datetime vectors
-dvec <- Sys.Date() + -2:2
-dtvec <- Sys.time() + (-2:2)*0.5
-
-# call the underlying C++ function
-result <- RcppDateExample(dvec, dtvec)
-
-# inspect returned object
-result
-}
-\keyword{programming}
-\keyword{interface}
Deleted: pkg/Rcpp/man/RcppVector.Rd
===================================================================
--- pkg/Rcpp/man/RcppVector.Rd 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/man/RcppVector.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,94 +0,0 @@
-\name{RcppVector}
-\alias{RcppVector}
-\alias{RcppMatrix}
-\alias{RcppStringVector}
-\alias{RcppVectorExample}
-\title{C++ classes for receiving R object in C++}
-\description{
- \code{RcppVector}, \code{RcppMatrix} and \code{RcppStringVector} are
- C++ classes defined in \code{Rcpp.h} that can
- pass vectors (matrices) of \R objects of appropriate types to C++ via
- the \code{.Call()} function interface.
-
- The vector and matrix types are templated and can operate on \R types
- \code{intger} and \code{numeric}.
-
- Member functions are provided to query the dimension of the vector or
- matrix object, convert it in a corresponding \code{C} representation,
- and also to convert it into a corresponding STL object.
-
-}
-%\usage{
-%}
-%\arguments{
-%}
-%\value{
-% Internal to the C++ code.
-%}
-\details{
- Usage of \code{RcppVector}, \code{RcppMatrix} and
- \code{RcppStringVector} in \code{C++} is fully defined in
- \code{Rcpp.h}.
-
- As example, consider a call from \R to \code{C++} such as
-
- \preformatted{
- # an R example passing one type of each class to a function
- # someFunction in package somePackage
- val <- .Call("someFunction",
- rnorm(100), # numeric vector
- sample(1:10, 5, TRUE) # int vector
- search(), # character vector
- as.matrix(rnorm(100),10,10), # matrix
- PACKAGE="somePackage")
- }
-
- At the \code{C++} level, the corresponding code to assign these parameter to
- \code{C++} objects is can be as follows (taken from the C++ source of
- \code{RcppExample}):
- \preformatted{%
- SEXP someFunction(SEXP nvec, SEXP ivec,
- SEXP svec, SEXP nmat) {
-
- RcppVector<double> nv(nvec);
- RcppVector<int> iv(ivec);
- RcppStringVector sv(svec);
- RcppMatrix<double> nm(nmat);
- }
- }
-
- These \code{C++} objects could then be queried via
- \preformatted{%
- int n = nv.size();
- int d1 = nm.dim1(), d2 = nm.dim2();
- }
- to retrieve, respectively, vector length and matrix dimensions.
-
- Moreover, the \code{stlVector()} and \code{stlMatrix()} member
- functions can be used to convert the objects into STL objects:
- \preformatted{%
- vector<int> ivstl = iv.stlVector();
- vector< vector< double > > = nm.stlMatrix();
- }
-
-}
-%\references{
-% See \code{Rcpp.h} and the package vignette.
-%}
-\seealso{
- \code{RcppExample}, the vignette \dQuote{RcppAPI}.
-}
-\author{Dominick Samperi wrote most of Rcpp during 2005 and 2006. Dirk
- Eddelbuettel made some additions, and became maintainer in 2008.}
-\examples{
-# set up some value
-vector <- (seq(1,9))^2
-
-# call the underlying C++ function
-result <- RcppVectorExample(vector)
-
-# inspect returned object
-result
-}
-\keyword{programming}
-\keyword{interface}
Deleted: pkg/Rcpp/src/RcppExample.cpp
===================================================================
--- pkg/Rcpp/src/RcppExample.cpp 2010-02-16 16:17:03 UTC (rev 696)
+++ pkg/Rcpp/src/RcppExample.cpp 2010-02-16 16:21:27 UTC (rev 697)
@@ -1,478 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; -*-
-//
-// RcppExample.cpp: R/C++ interface class library example
-//
-// Copyright (C) 2005 - 2006 Dominick Samperi
-// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
-//
-// 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>
-
-/*
- * The following class definitions employ advanced features of the Rcpp
- * library and R, permitting the C++ programmer to call user-defined functions
- * on the R side. They should be skipped on first reading.
- */
-
-/*
- * Define a class that can be used to call an R function that expects a
- * real vector argument and returns a scalar. The R function is defined in
- * the example section of the documentation page for RcppExample (see
- * RcppExample.Rd).
- */
-class MyRVectorFunc : public RcppFunction {
-public:
- MyRVectorFunc(SEXP fn) : RcppFunction(fn) {}
-
- // This trivial function will use an R function to compute the
- // sum of the elements of v!
- double getSum(std::vector<double>& v) {
-
- // Turn vector into a SEXP that can be passed to R as an argument.
- setRVector(v);
-
- // Call the R function that was passed in as the paramter fn, with
- // the SEXP vector that was just set as its argument.
- SEXP result = vectorCall();
-
- // Assuming that the R function simply returns a real number we
- // pass it back to the C++ user as follows. If the R function returns
- // something more complicated transform result into a C++ object to
- // be returned, and clear the part of the protection stack due to
- // this object before returning (to prevent protection stack overflow).
- // Note that it is unsafe to do this if the returned result depends
- // on PROTECT-ed SEXP's. For example, result should not be
- // wrapped in a class like RcppParams where objects hold onto the
- // the PROTECT-ed SEXP that was used to construct them.
-
- double value = REAL(result)[0];
-
- // Safe now to clear the contribution of this function to the
- // protection stack.
- clearProtectionStack();
-
- return value;
- }
-};
-
-/*
- * Define a class that can be used to call an R function that expects a
- * heterogeneous list argument, and returns a vector of the same length
- * with 1 added to each component (no names). The R function is defined in
- * the example section of the documentation page for RcppExample (see
- * RcppExample.Rd).
- */
-class MyRListFunc : public RcppFunction {
-public:
- MyRListFunc(SEXP fn) : RcppFunction(fn) {}
- std::vector<double> addOne(double alpha, double beta, double gamma) {
-
- // Build argument list.
- setRListSize(3);
- appendToRList("alpha", alpha);
- appendToRList("beta", beta);
- appendToRList("gamma", gamma);
-
- // Call the R function passed in as fn with the list argument just
- // constructed.
- SEXP result = listCall();
-
- // Turn returned R vector into a C++ vector, clear protection stack,
- // and return.
- std::vector<double> vec(Rf_length(result));
- for(int i=0; i < Rf_length(result); i++)
- vec[i] = REAL(result)[i];
-
- // See comments in previous class definition on the purpose of this.
- clearProtectionStack();
-
- return vec;
- }
-};
-
-/*
- * Sample function illustrates how to use the Rcpp R/C++ interface library.
- */
-RcppExport SEXP Rcpp_Example(SEXP params, SEXP nlist,
- SEXP numvec, SEXP nummat,
- SEXP df, SEXP datevec, SEXP stringvec,
- SEXP fnvec, SEXP fnlist) {
-
- SEXP rl=R_NilValue; // Use this when there is nothing to be returned.
- char *exceptionMesg=NULL;
-
- try {
-
- int i=0, j=0;
-
- // Get parameters in params.
- RcppParams rparam(params);
- std::string method = rparam.getStringValue("method");
- double tolerance = rparam.getDoubleValue("tolerance");
- int maxIter = rparam.getIntValue("maxIter");
- RcppDate startDate = rparam.getDateValue("startDate");
-
- // The output of commands like this may not appear under Windows.
- Rprintf("Parsing start date argument: %d/%d/%d\n",
- startDate.getMonth(),
- startDate.getDay(),
- startDate.getYear());
-
- // QuantLib note: an RcppDate is automatically converted to QuantLib
- // Date when the context calls for this, provided
- // USING_QUANTLIB is set.
-
- RcppDateVector dateVec(datevec);
- //dateVec(0) = RcppDate(12, 15, 1989); // update one element.
-
- RcppStringVector stringVec(stringvec);
- //stringVec(1) = string("New York"); // update one element.
-
- // and nl.getValue(i) to fetch data.
- RcppNumList nl(nlist);
-
- // numvec parameter viewed as vector of ints (with truncation).
- //RcppVector<int> vecI(numvec);
-
- // mat parameter viewed as matrix of ints (with truncation).
- //RcppMatrix<int> matI(nummat);
-
- // vec parameter viewed as vector of doubles.
- RcppVector<double> vecD(numvec);
-
- // mat parameter viewed as matrix of doubles.
- RcppMatrix<double> matD(nummat);
-
- // Do some computations with the matrices.
- int nrows = matD.getDim1();
- int ncols = matD.getDim2();
- for(i = 0; i < nrows; i++)
- for(j = 0; j < ncols; j++)
- matD(i,j) = 2 * matD(i,j);
-
- int len = vecD.size();
- for(i = 0; i < len; i++)
- vecD(i) = 3 * vecD(i);
-
- // Get copy of matrix/vector in standard (unchecked) C/C++ format.
- // May be useful when these vectors need to be passed to
- // C/C++ code that does not know about Rcpp classes...
- double **a = matD.cMatrix();
- double *v = vecD.cVector();
-
- // ...or we might want to use an STL container...
- std::vector<double> stlvec(vecD.stlVector());
- nrows = (int)stlvec.size();
- for(i = 0; i < nrows; i++)
- stlvec[i] += 1;
-
- // ...or perhaps a container of containers.
- std::vector<std::vector<double> > stlmat(matD.stlMatrix());
- nrows = (int)stlmat.size();
- ncols = (int)stlmat[0].size();
- for(i = 0; i < nrows; i++)
- for(j = 0; j < ncols; j++)
- stlmat[i][j] += 2;
-
- // Get a zero matrix the same size as matD.
- //RcppMatrix<double> matZ(nrows, ncols);
-
- // Make a vector of strings
- std::vector<std::string> svec(2);
- svec[0] = "hello";
- svec[1] = "world";
-
- // Process the input data frame and show factors and dates.
- RcppFrame inframe(df);
-
- /*
- Rprintf("\nFactors and Dates in frame...");
- vector<vector<ColDatum> > table = inframe.getTableData();
- int nrow = table.size();
- int ncol = table[0].size();
- for(int row=0; row < nrow; row++) {
- for(int col=0; col < ncol; col++) {
- RcppDate d;
- string name;
- int level;
- switch(table[row][col].getType()) {
- case COLTYPE_FACTOR:
- level = table[row][col].getFactorLevel();
- name = table[row][col].getFactorLevelName();
- Rprintf("Level, name: %d, %s\n",
- level, name.c_str());
- break;
- case COLTYPE_DATE:
- d = table[row][col].getDateValue();
- Rprintf("Start Date: %d/%d/%d\n",
- d.getMonth(),
- d.getDay(),
- d.getYear());
- break;
- default:
- ; // Ignore other types.
- }
- }
- }
- */
-
- // Make a pre-data frame, that is, a list object that when passed
- // the the R function data.frame() will return a data frame with
- // the specified column names and data types. The first row added
- // determines the types for all columns.
- int numCol=4;
- std::vector<std::string> colNames(numCol);
- colNames[0] = "alpha"; // column of strings
- colNames[1] = "beta"; // column of reals
- colNames[2] = "gamma"; // factor column
- colNames[3] = "delta"; // column of Dates
- RcppFrame frame(colNames);
-
- // Third column will be a factor. In the current implementation the
- // level names are copied to every factor value (and factors
- // in the same column must have the same level names). The level names
- // for a particular column will be factored out (pardon the pun) in
- // a future release.
- int numLevels = 2;
- std::string *levelNames = new std::string[2];
- levelNames[0] = std::string("pass"); // level 1
- levelNames[1] = std::string("fail"); // level 2
-
- // First row (this one determines column types).
- std::vector<ColDatum> row1(numCol);
- row1[0].setStringValue("a");
- row1[1].setDoubleValue(3.14);
- row1[2].setFactorValue(levelNames, numLevels, 1);
- row1[3].setDateValue(RcppDate(7,4,2006));
- frame.addRow(row1);
-
- // Second row.
- std::vector<ColDatum> row2(numCol);
- row2[0].setStringValue("b");
- row2[1].setDoubleValue(6.28);
- row2[2].setFactorValue(levelNames, numLevels, 1);
- row2[3].setDateValue(RcppDate(12,25,2006));
- frame.addRow(row2);
-
- // Done with levelNames.
- delete [] levelNames;
-
- // Test MyRVectorFunction defined above...
- MyRVectorFunc vfunc(fnvec);
- int n = 10;
- std::vector<double> vecInput(n);
- for(int i=0; i < n; i++)
- vecInput[i] = i;
- double vecSum = vfunc.getSum(vecInput);
- Rprintf("Testing vector function argument: vecSum = %lf\n", vecSum);
-
- // Test MyRListFunction defined above...
- MyRListFunc lfunc(fnlist);
- double alpha=1, beta=2, gamma=3;
- std::vector<double> vecOut = lfunc.addOne(alpha, beta, gamma);
- Rprintf("Testing list function argument: %lf, %lf, %lf\n", vecOut[0], vecOut[1], vecOut[2]);
-
- RcppDate aDate(12, 25, 1999);
-
- // Build result set to be returned as a list to R.
- RcppResultSet rs;
-
- rs.add("date", aDate);
- rs.add("dateVec", dateVec);
- rs.add("method", method);
- rs.add("tolerance", tolerance);
- rs.add("maxIter", maxIter);
- rs.add("nlFirstName", nl.getName(0));
- rs.add("nlFirstValue", nl.getValue(0));
- rs.add("matD", matD);
- rs.add("stlvec", stlvec);
- rs.add("stlmat", stlmat);
- rs.add("a", a, nrows, ncols);
- rs.add("v", v, len);
- rs.add("stringVec", stringVec);
- rs.add("strings", svec);
- rs.add("InputDF", inframe);
- rs.add("PreDF", frame);
-
-
- // Instead of returning selected input parameters as we did in
- // the last three statements, the entire input parameter list
- // can be returned like this:
- rs.add("params", params, false);
-
-
- // 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;
-}
-
-
-RcppExport SEXP RcppParamsExample(SEXP params) {
-
- SEXP rl=R_NilValue; // Use this when there is nothing to be returned.
- char *exceptionMesg=NULL;
-
- try {
-
- // Get parameters in params.
- RcppParams rparam(params);
- std::string method = rparam.getStringValue("method");
- double tolerance = rparam.getDoubleValue("tolerance");
- int maxIter = rparam.getIntValue("maxIter");
- RcppDate startDate = rparam.getDateValue("startDate");
-
- Rprintf("\nIn C++, seeing the following value\n");
- Rprintf("Method argument : %s\n", method.c_str());
- Rprintf("Tolerance argument : %f\n", tolerance);
- Rprintf("MaxIter argument : %d\n", maxIter);
- Rprintf("Start date argument: %04d-%02d-%02d\n",
- startDate.getYear(), startDate.getMonth(), startDate.getDay());
-
- // Build result set to be returned as a list to R.
- RcppResultSet rs;
-
- rs.add("method", method);
- rs.add("tolerance", tolerance);
- rs.add("maxIter", maxIter);
- rs.add("startDate", startDate);
-
- // Instead of returning selected input parameters as we did in
- // the last statements, the entire input parameter list can be
- // returned like this:
- rs.add("params", params, false); // 'false' to not PROTECT params
-
- // 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;
-}
-
-RcppExport SEXP RcppDateExample(SEXP dvsexp, SEXP dtvsexp) {
-
- SEXP rl=R_NilValue; // Use this when there is nothing to be returned.
- char *exceptionMesg=NULL;
-
- try {
-
- RcppDateVector dv(dvsexp);
- RcppDatetimeVector dtv(dtvsexp);
-
- Rprintf("\nIn C++, seeing the following date value\n");
- for (int i=0; i<dv.size(); i++) {
- std::cout << dv(i) << std::endl;
- dv(i) = dv(i) + 7; // shift a week
- }
- Rprintf("\nIn C++, seeing the following datetime value\n");
- for (int i=0; i<dtv.size(); i++) {
- std::cout << dtv(i) << std::endl;
- dtv(i) = dtv(i) + 0.250; // shift 250 millisec
- }
-
- // Build result set to be returned as a list to R.
- RcppResultSet rs;
- rs.add("date", dv);
- rs.add("datetime", dtv);
-
- // 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;
-}
-
-RcppExport SEXP RcppVectorExample(SEXP vector) {
-
- SEXP rl=R_NilValue; // Use this when there is nothing to be returned.
- char *exceptionMesg=NULL;
-
- try {
-
- // Get parameters in params.
- RcppVector<int> vec(vector);
- int n = vec.size();
-
- Rprintf("\nIn C++, seeing a vector of length %d\n", n);
-
- // create a C++ STL vector, and reserve appropriate size
- std::vector<double> res(n);
-
- for (int i=0; i<n; i++) {
- res[i] = sqrt(static_cast<double>(vec(i)));
- }
-
- // Build result set to be returned as a list to R.
- RcppResultSet rs;
-
- rs.add("result", res);
- rs.add("original", vec);
-
- // 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;
-}
-
-RcppExport SEXP RcppXPtrExample_create_external_pointer(){
- std::vector<int> *v = new std::vector<int> ;
- v->push_back( 1 ) ;
- v->push_back( 2 ) ;
- Rcpp::XPtr< std::vector<int> > p(v) ;
- return p ;
-}
-
-RcppExport SEXP RcppXPtrExample_get_external_pointer(SEXP x){
- Rcpp::XPtr< std::vector<int> > p(x) ;
- return Rf_ScalarInteger( p->back( ) ) ;
-}
-
-
Copied: pkg/RcppExamples/man/RcppDate.Rd (from rev 694, pkg/Rcpp/man/RcppDate.Rd)
===================================================================
--- pkg/RcppExamples/man/RcppDate.Rd (rev 0)
+++ pkg/RcppExamples/man/RcppDate.Rd 2010-02-16 16:21:27 UTC (rev 697)
@@ -0,0 +1,91 @@
+\name{RcppDate}
+\alias{RcppDate}
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/rcpp -r 697
More information about the Rcpp-commits
mailing list