From 5a0edee3616b512895774f3903bea4f1d35ba991 Mon Sep 17 00:00:00 2001 From: James J Balamuta Date: Fri, 15 Jul 2016 14:37:53 -0500 Subject: [PATCH 1/2] Added section on default function parameters. Addressed #418 --- ChangeLog | 4 +++ vignettes/Rcpp-FAQ.Rnw | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/ChangeLog b/ChangeLog index a2a869955..79e663cf7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2016-07-15 James J Balamuta + + * vignettes/Rcpp-FAQ.Rnw: Added section on default function parameters + 2016-07-15 Dirk Eddelbuettel * vignettes/Rcpp-FAQ.Rnw: Also point to Rcpp-attributes in Question 1 diff --git a/vignettes/Rcpp-FAQ.Rnw b/vignettes/Rcpp-FAQ.Rnw index 27c81919e..91895e7ee 100644 --- a/vignettes/Rcpp-FAQ.Rnw +++ b/vignettes/Rcpp-FAQ.Rnw @@ -693,6 +693,61 @@ exposure to a number of advanced Rcpp users. The Several dozen fully documented examples are provided at the \href{http://gallery.rcpp.org}{Rcpp Gallery} -- which is also open for new contributions. +\subsection{Can I use default function parameters with \pkg{Rcpp}?} + +Yes, you can use default parameters with \textit{some} limitations. +The limitations are mainly related to string literals and empty vectors. +This is what is currently supported: + +\begin{itemize} +\item String literals delimited by quotes (e.g. \code{"foo"}) +\item Integer and Decimal numeric values (e.g. \code{10} or \code{4.5}) +\item Pre-defined constants including: +\begin{itemize} +\item Booleans: \code{true} and \code{false} +\item Null Values: \code{R_NilValue}, \code{NA_STRING}, +\code{NA_INTEGER}, \code{NA_REAL}, and \code{NA_LOGICAL}. +\end{itemize} +\item Selected vector types can be instantiated using the empty form of the +\code{::create} static member function. +\begin{itemize} +\item \code{CharacterVector}, \code{IntegerVector}, and +\code{NumericVector} +\end{itemize} +\item Matrix types instantiated using the rows, cols constructor \code{Rcpp::Matrix n(rows,cols)} +\begin{itemize} +\item \code{CharacterMatrix}, \code{IntegerMatrix}, and +\code{NumericMatrix}) +\end{itemize} +\end{itemize} + +To illustrate, please consider the following example that provides a short +how to: + +<>= +#include + +// [[Rcpp::export]] +void sample_defaults(NumericVector x = NumericVector::create(), // Size 0 vector + bool bias = true, // Set to true + std::string method = "rcpp rules!"){ // Default string + Rcpp::Rcout << "x size: " << x.size() << ", "; + Rcpp::Rcout << "bias value: " << bias << ", "; + Rcpp::Rcout << "method value: " << method << std::endl; +} + +/*** R +sample_defaults() # all defaults +sample_defaults(1:5) # supply x values +sample_defaults(bias = FALSE, + method = "rstats") # supply bool and string +*/ +@ + +Note: In \code{cpp}, the default \code{bool} values are \code{true} and +\code{false} whereas in R the valid types are \code{TRUE} or \code{FALSE}. + + \subsection{Can I use templates with \pkg{Rcpp} ? } \begin{quote} From 895ba4441fbd049daf2c2e8e57318afd5db98286 Mon Sep 17 00:00:00 2001 From: James J Balamuta Date: Fri, 15 Jul 2016 15:04:53 -0500 Subject: [PATCH 2/2] Moved section down & fixed syntax highlight on old max post --- vignettes/Rcpp-FAQ.Rnw | 113 +++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/vignettes/Rcpp-FAQ.Rnw b/vignettes/Rcpp-FAQ.Rnw index 91895e7ee..8d2e291f9 100644 --- a/vignettes/Rcpp-FAQ.Rnw +++ b/vignettes/Rcpp-FAQ.Rnw @@ -668,9 +668,9 @@ library paths. A solution is outlined In short, you want to add this entry to your \texttt{~/.R/Makevars}: -\begin{verbatim} +<>= FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'` -\end{verbatim} +@ This invocation explicitly asks and constructs the library link paths from the \texttt{gfortran}'s reported search paths, and produces a set @@ -693,60 +693,6 @@ exposure to a number of advanced Rcpp users. The Several dozen fully documented examples are provided at the \href{http://gallery.rcpp.org}{Rcpp Gallery} -- which is also open for new contributions. -\subsection{Can I use default function parameters with \pkg{Rcpp}?} - -Yes, you can use default parameters with \textit{some} limitations. -The limitations are mainly related to string literals and empty vectors. -This is what is currently supported: - -\begin{itemize} -\item String literals delimited by quotes (e.g. \code{"foo"}) -\item Integer and Decimal numeric values (e.g. \code{10} or \code{4.5}) -\item Pre-defined constants including: -\begin{itemize} -\item Booleans: \code{true} and \code{false} -\item Null Values: \code{R_NilValue}, \code{NA_STRING}, -\code{NA_INTEGER}, \code{NA_REAL}, and \code{NA_LOGICAL}. -\end{itemize} -\item Selected vector types can be instantiated using the empty form of the -\code{::create} static member function. -\begin{itemize} -\item \code{CharacterVector}, \code{IntegerVector}, and -\code{NumericVector} -\end{itemize} -\item Matrix types instantiated using the rows, cols constructor \code{Rcpp::Matrix n(rows,cols)} -\begin{itemize} -\item \code{CharacterMatrix}, \code{IntegerMatrix}, and -\code{NumericMatrix}) -\end{itemize} -\end{itemize} - -To illustrate, please consider the following example that provides a short -how to: - -<>= -#include - -// [[Rcpp::export]] -void sample_defaults(NumericVector x = NumericVector::create(), // Size 0 vector - bool bias = true, // Set to true - std::string method = "rcpp rules!"){ // Default string - Rcpp::Rcout << "x size: " << x.size() << ", "; - Rcpp::Rcout << "bias value: " << bias << ", "; - Rcpp::Rcout << "method value: " << method << std::endl; -} - -/*** R -sample_defaults() # all defaults -sample_defaults(1:5) # supply x values -sample_defaults(bias = FALSE, - method = "rstats") # supply bool and string -*/ -@ - -Note: In \code{cpp}, the default \code{bool} values are \code{true} and -\code{false} whereas in R the valid types are \code{TRUE} or \code{FALSE}. - \subsection{Can I use templates with \pkg{Rcpp} ? } @@ -1238,6 +1184,61 @@ concering data.frame creation with \pkg{Rcpp}. One solution offers a custom \code{ListBuilder} class to circumvent the limit; another suggests to simply nest lists. +\subsection{Can I use default function parameters with \pkg{Rcpp}?} + +Yes, you can use default parameters with \textit{some} limitations. +The limitations are mainly related to string literals and empty vectors. +This is what is currently supported: + +\begin{itemize} +\item String literals delimited by quotes (e.g. \code{"foo"}) +\item Integer and Decimal numeric values (e.g. \code{10} or \code{4.5}) +\item Pre-defined constants including: +\begin{itemize} +\item Booleans: \code{true} and \code{false} +\item Null Values: \code{R_NilValue}, \code{NA_STRING}, +\code{NA_INTEGER}, \code{NA_REAL}, and \code{NA_LOGICAL}. +\end{itemize} +\item Selected vector types can be instantiated using the empty form of the +\code{::create} static member function. +\begin{itemize} +\item \code{CharacterVector}, \code{IntegerVector}, and +\code{NumericVector} +\end{itemize} +\item Matrix types instantiated using the rows, cols constructor \code{Rcpp::Matrix n(rows,cols)} +\begin{itemize} +\item \code{CharacterMatrix}, \code{IntegerMatrix}, and +\code{NumericMatrix}) +\end{itemize} +\end{itemize} + +To illustrate, please consider the following example that provides a short +how to: + +<>= +#include + +// [[Rcpp::export]] +void sample_defaults(NumericVector x = NumericVector::create(), // Size 0 vector + bool bias = true, // Set to true + std::string method = "rcpp rules!"){ // Default string + Rcpp::Rcout << "x size: " << x.size() << ", "; + Rcpp::Rcout << "bias value: " << bias << ", "; + Rcpp::Rcout << "method value: " << method << std::endl; +} + +/*** R +sample_defaults() # all defaults +sample_defaults(1:5) # supply x values +sample_defaults(bias = FALSE, + method = "rstats") # supply bool and string +*/ +@ + +Note: In \code{cpp}, the default \code{bool} values are \code{true} and +\code{false} whereas in R the valid types are \code{TRUE} or \code{FALSE}. + + \section{Support}