Skip to content

Commit 5a0edee

Browse files
committed
Added section on default function parameters.
Addressed #418
1 parent a4de77f commit 5a0edee

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2016-07-15 James J Balamuta <[email protected]>
2+
3+
* vignettes/Rcpp-FAQ.Rnw: Added section on default function parameters
4+
15
2016-07-15 Dirk Eddelbuettel <[email protected]>
26

37
* vignettes/Rcpp-FAQ.Rnw: Also point to Rcpp-attributes in Question 1

vignettes/Rcpp-FAQ.Rnw

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,61 @@ exposure to a number of advanced Rcpp users. The
693693
Several dozen fully documented examples are provided at the
694694
\href{http://gallery.rcpp.org}{Rcpp Gallery} -- which is also open for new contributions.
695695

696+
\subsection{Can I use default function parameters with \pkg{Rcpp}?}
697+
698+
Yes, you can use default parameters with \textit{some} limitations.
699+
The limitations are mainly related to string literals and empty vectors.
700+
This is what is currently supported:
701+
702+
\begin{itemize}
703+
\item String literals delimited by quotes (e.g. \code{"foo"})
704+
\item Integer and Decimal numeric values (e.g. \code{10} or \code{4.5})
705+
\item Pre-defined constants including:
706+
\begin{itemize}
707+
\item Booleans: \code{true} and \code{false}
708+
\item Null Values: \code{R_NilValue}, \code{NA_STRING},
709+
\code{NA_INTEGER}, \code{NA_REAL}, and \code{NA_LOGICAL}.
710+
\end{itemize}
711+
\item Selected vector types can be instantiated using the empty form of the
712+
\code{::create} static member function.
713+
\begin{itemize}
714+
\item \code{CharacterVector}, \code{IntegerVector}, and
715+
\code{NumericVector}
716+
\end{itemize}
717+
\item Matrix types instantiated using the rows, cols constructor \code{Rcpp::<Type>Matrix n(rows,cols)}
718+
\begin{itemize}
719+
\item \code{CharacterMatrix}, \code{IntegerMatrix}, and
720+
\code{NumericMatrix})
721+
\end{itemize}
722+
\end{itemize}
723+
724+
To illustrate, please consider the following example that provides a short
725+
how to:
726+
727+
<<lang=cpp>>=
728+
#include <Rcpp.h>
729+
730+
// [[Rcpp::export]]
731+
void sample_defaults(NumericVector x = NumericVector::create(), // Size 0 vector
732+
bool bias = true, // Set to true
733+
std::string method = "rcpp rules!"){ // Default string
734+
Rcpp::Rcout << "x size: " << x.size() << ", ";
735+
Rcpp::Rcout << "bias value: " << bias << ", ";
736+
Rcpp::Rcout << "method value: " << method << std::endl;
737+
}
738+
739+
/*** R
740+
sample_defaults() # all defaults
741+
sample_defaults(1:5) # supply x values
742+
sample_defaults(bias = FALSE,
743+
method = "rstats") # supply bool and string
744+
*/
745+
@
746+
747+
Note: In \code{cpp}, the default \code{bool} values are \code{true} and
748+
\code{false} whereas in R the valid types are \code{TRUE} or \code{FALSE}.
749+
750+
696751
\subsection{Can I use templates with \pkg{Rcpp} ? }
697752

698753
\begin{quote}

0 commit comments

Comments
 (0)