@@ -116,26 +116,25 @@ Rcpp.package.skeleton("mypackage")
116116\begin {Hchunk }
117117\begin {verbatim }
118118$ ls -1R mypackage/
119- mypackage/:
120119DESCRIPTION
121- man
122120NAMESPACE
123121R
124122Read-and-delete-me
123+ man
125124src
126125
126+ mypackage/R:
127+ RcppExports.R
128+
127129mypackage/man:
128130mypackage-package.Rd
129131rcpp_hello_world.Rd
130132
131- mypackage/R:
132- rcpp_hello_world.R
133-
134133mypackage/src:
135134Makevars
136135Makevars.win
136+ RcppExports.cpp
137137rcpp_hello_world.cpp
138- rcpp_hello_world.h
139138$
140139\end {verbatim }
141140\end {Hchunk }
@@ -145,82 +144,87 @@ as it fulfills two roles. It creates the complete set of files needed for a
145144package, and it also includes the different components needed for using
146145\pkg {Rcpp} that we discuss in the following sections.
147146
148- \subsection {\proglang {R } code }
147+ \subsection {\proglang {C++ } code }
149148
150- The skeleton contains an example \proglang {R} function
151- \texttt {rcpp\_ hello\_ world } that uses the \Sexpr{link(" .Call" )} interface to
152- invoke the \proglang {C++} function \texttt {rcpp\_ hello\_ world } from the
153- package \texttt {mypackage }.
149+ If the \texttt {attributes } argument is set to
150+ \texttt {TRUE }\footnote {Setting \texttt {attributes } to \texttt {TRUE } is the default. This document
151+ does not cover the behavior of \texttt {Rcpp.package.skeleton } when \texttt {attributes } is set
152+ to \texttt {FALSE } as we try to encourage package developpers to use
153+ attributes. },
154+ the following \proglang {C++} file is included in the \texttt {src/ } directory:
155+
156+ <<lang =cpp >>=
157+ # include <Rcpp.h>
158+ using namespace Rcpp ;
159+
160+ // [[Rcpp :: export ]]
161+ List rcpp_hello_world() {
162+
163+ CharacterVector x = CharacterVector :: create( " foo" , " bar" ) ;
164+ NumericVector y = NumericVector :: create( 0.0 , 1.0 ) ;
165+ List z = List :: create( x , y ) ;
154166
155- <<lang =r >>=
156- rcpp_hello_world <- function (){
157- .Call( " rcpp_hello_world" , PACKAGE = " mypackage" )
167+ return z ;
158168}
159169@
160170
161- \pkg {Rcpp} uses the \Sexpr{link(" .Call" )} calling convention as it allows
162- transport of actual \proglang {R} objects back and forth between the
163- \proglang {R} side and the \proglang {C++} side. \proglang {R} objects
164- (\texttt {SEXP }) can be conveniently manipulated using the \pkg {Rcpp} API.
165-
166- Note that in this example, no arguments were passed from \proglang {R} down to
167- the \proglang {C++} layer. Doing so is straightforward (and one of the key
168- features of \pkg {Rcpp}) but not central to our discussion of the package
169- creation mechanics.
171+ The file defines the simple \texttt {rcpp\_ hello\_ world } function that
172+ uses a few \pkg {Rcpp} classes and returns a \texttt {List }.
170173
171- \subsection {\proglang {C++} code }
174+ This function is preceded by the \texttt {Rcpp::export } attribute to automatically
175+ handle argument conversion because \proglang {R} has to be taught how to
176+ e.g. handle the \texttt {List } class.
172177
173- The \proglang {C++} function is declared in the \texttt { rcpp \_ hello \_ world.h }
174- header file:
178+ \Sexpr{link( " Rcpp.package.skeleton " )} then invokes \Sexpr{link( " compileAttributes " ) }
179+ on the package, which generates the \texttt { RcppExports.cpp } file:
175180
176181<<lang =cpp >>=
177- # ifndef _mypackage_RCPP_HELLO_WORLD_H
178- # define _mypackage_RCPP_HELLO_WORLD_H
182+ // This file was generated by Rcpp :: compileAttributes
183+ // Generator token : 10BE3573 - 1514 - 4C36 - 9D1C - 5A225CD40393
179184
180185# include <Rcpp.h>
181186
182- /*
183- * note : RcppExport is an alias to `extern "C"` defined by Rcpp.
184- *
185- * It gives C calling convention to the rcpp_hello_world function so that
186- * it can be called from .Call in R. Otherwise , the C ++ compiler mangles the
187- * name of the function and .Call cannot find it.
188- *
189- * It is only useful to use RcppExport when the function is intended to be called
190- * by .Call. See the thread http : // thread.gmane.org / gmane.comp.lang.r.rcpp / 649 / focus = 672
191- * on Rcpp - devel for a misuse of RcppExport
192- */
193- RcppExport SEXP rcpp_hello_world() ;
194-
195- # endif
187+ using namespace Rcpp ;
188+
189+ // rcpp_hello_world
190+ List rcpp_hello_world();
191+ RcppExport SEXP mypackage_rcpp_hello_world() {
192+ BEGIN_RCPP
193+ SEXP __sexp_result ;
194+ {
195+ Rcpp :: RNGScope __rngScope ;
196+ List __result = rcpp_hello_world();
197+ PROTECT(__sexp_result = Rcpp :: wrap(__result ));
198+ }
199+ UNPROTECT(1 );
200+ return __sexp_result ;
201+ END_RCPP
202+ }
196203@
197204
198- The header includes the \texttt {Rcpp.h } file, which is the only file that
199- needs to be included to use \pkg {Rcpp}. The function is then implemented in
200- the \texttt {rcpp\_ hello\_ world.cpp } file
205+ This file defines a function with the appropriate calling convention, suitable for
206+ \Sexpr{link(" .Call" )} . It needs to be regenerated each time functions
207+ exposed by attributes are modified. This is the task of the
208+ \Sexpr{link(" compileAttributes" )} function. A discussion on attributes is
209+ beyond the scope of this document and more information is available
210+ in the attributes vignette \citep {CRAN:Rcpp:Attributes }.
201211
202- <<lang =cpp >>=
203- # include "rcpp_hello_world.h"
212+ \subsection {\proglang {R} code }
204213
205- SEXP rcpp_hello_world(){
206- using namespace Rcpp ;
214+ The \Sexpr{link( " compileAttributes " )} also generates \proglang {R} code
215+ that uses the \proglang {C++} function.
207216
208- CharacterVector x = CharacterVector :: create( " foo " , " bar " ) ;
209- NumericVector y = NumericVector :: create( 0.0 , 1.0 ) ;
210- List z = List :: create( x , y ) ;
217+ << lang = cpp >> =
218+ # This file was generated by Rcpp::compileAttributes
219+ # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
211220
212- return z ;
221+ rcpp_hello_world <- function () {
222+ .Call(' mypackage_rcpp_hello_world' , PACKAGE = ' mypackage' )
213223}
214224@
215225
216- The function creates an \proglang {R} list that contains a
217- \Sexpr{link(" character" )} vector and a \Sexpr{link(" numeric" )} vector using \pkg {Rcpp}
218- classes. At the \proglang {R} level, we will therefore receive a list of
219- length two containing these two vectors.
220-
221- <<eval =FALSE >>=
222- rcpp_hello_world( )
223- @
226+ This is also a generated file so it should not be modified manually, rather
227+ regenerated as needed by \Sexpr{link(" compileAttributes" )} .
224228
225229\subsection {\texttt {DESCRIPTION } }
226230
@@ -317,7 +321,7 @@ contained in the package we are creating via
317321\Sexpr{link(" Rcpp.package.skeleton" )} will be loaded and thereby made
318322available to the newly created \proglang {R} package. Second, it declares
319323which functions should be globally visible from the namespace of this
320- package. As a reasonable default, we export all functions.
324+ package. As a reasonable default, we export all functions.
321325
322326\subsection {Help files }
323327
@@ -403,19 +407,22 @@ rcpp_hello_world()
403407\end {verbatim }
404408\end {Hchunk }
405409
410+ \section {Using modules }
411+
412+ This document does not cover the use of the \texttt {module } argument
413+ of \Sexpr{link(" Rcpp.package.skeleton" )} . It is covered
414+ in the modules vignette \citep {CRAN:Rcpp:Modules }.
406415
407416\section {Further examples }
408417
409418The canonical example of a package that uses \pkg {Rcpp} is the
410419\pkg {RcppExamples} \citep {CRAN:RcppExamples } package. \pkg {RcppExamples}
411- contains various examples of using \pkg {Rcpp} using both the extended
412- (`` new'' ) API and the older (`` classic'' ) API. Hence, the \pkg {RcppExamples}
420+ contains various examples of using \pkg {Rcpp}. Hence, the \pkg {RcppExamples}
413421package is provided as a template for employing \pkg {Rcpp} in packages.
414422
415423Other CRAN packages using the \pkg {Rcpp} package are \pkg {RcppArmadillo}
416- \citep {CRAN:RcppArmadillo }, \pkg {highlight} \citep {CRAN:highlight },
417- and \pkg {minqa} \citep {CRAN:minqa } all of which follow precisely the guidelines
418- of this document. Several other packages follow older (but still supported
424+ \citep {CRAN:RcppArmadillo },
425+ and \pkg {minqa} \citep {CRAN:minqa }. Several other packages follow older (but still supported
419426and appropriate) instructions. They can serve examples on how to get data to
420427and from \proglang {C++} routines, but should not be considered templates for
421428how to connect to \pkg {Rcpp}. The full list of packages using \pkg {Rcpp} can
0 commit comments