Skip to content

Commit 406befa

Browse files
authored
Merge pull request #505 from coatless/faq-default-parameters
Added section on default function parameters.
2 parents a4de77f + 895ba44 commit 406befa

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
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: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ library paths. A solution is outlined
668668

669669
In short, you want to add this entry to your \texttt{~/.R/Makevars}:
670670

671-
\begin{verbatim}
671+
<<lang=bash>>=
672672
FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`
673-
\end{verbatim}
673+
@
674674

675675
This invocation explicitly asks and constructs the library link paths
676676
from the \texttt{gfortran}'s reported search paths, and produces a set
@@ -693,6 +693,7 @@ 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+
696697
\subsection{Can I use templates with \pkg{Rcpp} ? }
697698

698699
\begin{quote}
@@ -1183,6 +1184,61 @@ concering data.frame creation with \pkg{Rcpp}. One solution offers a custom
11831184
\code{ListBuilder} class to circumvent the limit; another suggests to simply
11841185
nest lists.
11851186

1187+
\subsection{Can I use default function parameters with \pkg{Rcpp}?}
1188+
1189+
Yes, you can use default parameters with \textit{some} limitations.
1190+
The limitations are mainly related to string literals and empty vectors.
1191+
This is what is currently supported:
1192+
1193+
\begin{itemize}
1194+
\item String literals delimited by quotes (e.g. \code{"foo"})
1195+
\item Integer and Decimal numeric values (e.g. \code{10} or \code{4.5})
1196+
\item Pre-defined constants including:
1197+
\begin{itemize}
1198+
\item Booleans: \code{true} and \code{false}
1199+
\item Null Values: \code{R_NilValue}, \code{NA_STRING},
1200+
\code{NA_INTEGER}, \code{NA_REAL}, and \code{NA_LOGICAL}.
1201+
\end{itemize}
1202+
\item Selected vector types can be instantiated using the empty form of the
1203+
\code{::create} static member function.
1204+
\begin{itemize}
1205+
\item \code{CharacterVector}, \code{IntegerVector}, and
1206+
\code{NumericVector}
1207+
\end{itemize}
1208+
\item Matrix types instantiated using the rows, cols constructor \code{Rcpp::<Type>Matrix n(rows,cols)}
1209+
\begin{itemize}
1210+
\item \code{CharacterMatrix}, \code{IntegerMatrix}, and
1211+
\code{NumericMatrix})
1212+
\end{itemize}
1213+
\end{itemize}
1214+
1215+
To illustrate, please consider the following example that provides a short
1216+
how to:
1217+
1218+
<<lang=cpp>>=
1219+
#include <Rcpp.h>
1220+
1221+
// [[Rcpp::export]]
1222+
void sample_defaults(NumericVector x = NumericVector::create(), // Size 0 vector
1223+
bool bias = true, // Set to true
1224+
std::string method = "rcpp rules!"){ // Default string
1225+
Rcpp::Rcout << "x size: " << x.size() << ", ";
1226+
Rcpp::Rcout << "bias value: " << bias << ", ";
1227+
Rcpp::Rcout << "method value: " << method << std::endl;
1228+
}
1229+
1230+
/*** R
1231+
sample_defaults() # all defaults
1232+
sample_defaults(1:5) # supply x values
1233+
sample_defaults(bias = FALSE,
1234+
method = "rstats") # supply bool and string
1235+
*/
1236+
@
1237+
1238+
Note: In \code{cpp}, the default \code{bool} values are \code{true} and
1239+
\code{false} whereas in R the valid types are \code{TRUE} or \code{FALSE}.
1240+
1241+
11861242

11871243
\section{Support}
11881244

0 commit comments

Comments
 (0)