25
25
26
26
\newcommand {\proglang }[1]{\textsf {#1}}
27
27
\newcommand {\pkg }[1]{{\fontseries {b}\selectfont #1}}
28
+ \newcommand {\code }[1]{\texttt {#1 }}
29
+ \newcommand {\rdoc }[2]{\href {http://www.rdocumentation.org/packages/#1/functions/#2}{\code {#2}}}
28
30
29
31
<<version ,echo =FALSE ,print =FALSE >>=
30
32
prettyVersion <- packageDescription(" Rcpp" )$ Version
31
33
prettyDate <- format(Sys.Date(), " %B %e, %Y" )
34
+ require(Rcpp )
35
+ require(highlight )
32
36
@
33
37
34
38
\author {Dirk Eddelbuettel \and Romain Fran\c {c}ois}
35
39
\title {Writing a package that uses \pkg {Rcpp} }
36
40
\date {\pkg {Rcpp} version \Sexpr{prettyVersion } as of \Sexpr{prettyDate } }
37
41
38
- <<link ,echo =FALSE >>=
39
- require(Rcpp )
40
- require(highlight )
41
- link <- function ( f , package , text = f , root = " http://finzi.psych.upenn.edu/R/library/" ) {
42
- h <- if ( missing(package ) ) {
43
- as.character( help( f ) )
44
- } else {
45
- as.character( help( f , package = paste( package , sep = " " ) ) )
46
- }
47
- if ( ! length(h ) ){
48
- sprintf( " \\\\ textbf{%s}" , f )
49
- } else {
50
- rx <- " ^.*/([^/]*?)/help/(.*?)$"
51
- package <- sub( rx , " \\ 1" , h , perl = TRUE )
52
- page <- sub( rx , " \\ 2" , h , perl = TRUE )
53
- sprintf( " \\\\ href{%s%s/html/%s.html}{\\\\ texttt{%s}}" , root , package , page , text )
54
- }
55
- }
56
- linkS4class <- function (cl , package , text = cl , root = " http://finzi.psych.upenn.edu/R/library/" ) {
57
- link( sprintf(" %s-class" , cl ), package , text , root )
58
- }
59
- @
60
-
61
-
62
42
63
43
\begin {document }
64
44
\maketitle
65
45
66
46
\abstract {
67
47
\noindent This document provides a short overview of how to use
68
- \pkg {Rcpp}~\citep {CRAN:Rcpp ,JSS:Rcpp } when writing an \proglang {R} package. It
69
- shows how usage of the function \Sexpr{link(" Rcpp.package.skeleton" )}
70
- which creates a complete and self-sufficient example package using
71
- \pkg {Rcpp}. All components of the directory tree created by
72
- \Sexpr{link(" Rcpp.package.skeleton" )} are discussed in detail. This
73
- document thereby complements the \textsl {Writing R Extensions}
74
- manual~\citep {R:Extensions } which is the authoritative source on how to extend
75
- \proglang {R} in general.
76
- }
48
+ \pkg {Rcpp}~\citep {CRAN:Rcpp ,JSS:Rcpp ,Eddelbuettel:2013:Rcpp } when writing
49
+ an \proglang {R} package. It shows how usage of the function
50
+ \rdoc {Rcpp}{Rcpp.package.skeleton} which creates a complete and
51
+ self-sufficient example package using \pkg {Rcpp}. All components of the
52
+ directory tree created by \rdoc {Rcpp}{Rcpp.package.skeleton} are discussed
53
+ in detail. This document thereby complements the \textsl {Writing R
54
+ Extensions} manual~\citep {R:Extensions } which is the authoritative source
55
+ on how to extend \proglang {R} in general. }
77
56
78
57
\section {Introduction }
79
58
80
- \pkg {Rcpp}~\citep {CRAN:Rcpp ,JSS:Rcpp } is an extension package for \proglang {R} which
81
- offers an easy-to-use yet featureful interface between \proglang {C++} and
82
- \proglang {R}. However, it is somewhat different from a traditional
83
- \proglang {R} package because its key component is a \proglang {C++} library. A
84
- client package that wants to make use of the \pkg {Rcpp} features must link
85
- against the library provided by \pkg {Rcpp}.
59
+ \pkg {Rcpp}~\citep {CRAN:Rcpp ,JSS:Rcpp , Eddelbuettel:2013:Rcpp } is an extension
60
+ package for \proglang {R} which offers an easy-to-use yet featureful interface
61
+ between \proglang {C++} and \proglang { R}. However, it is somewhat different
62
+ from a traditional \proglang {R} package because its key component is a
63
+ \proglang {C++} library. A client package that wants to make use of the
64
+ \pkg {Rcpp} features must link against the library provided by \pkg {Rcpp}.
86
65
87
66
It should be noted that \proglang {R} has only limited support for
88
67
\proglang {C(++)}-level dependencies between packages~\citep {R:Extensions }. The
@@ -91,7 +70,7 @@ allows the client package to retrieve the headers of the target package (here
91
70
\pkg {Rcpp}), but support for linking against a library is not provided by
92
71
\proglang {R} and has to be added manually.
93
72
94
- This document follows the steps of the \Sexpr{link( " Rcpp.package.skeleton" ) }
73
+ This document follows the steps of the \rdoc { Rcpp}{Rcpp .package.skeleton}
95
74
function to illustrate a recommended way of using \pkg {Rcpp} from a client
96
75
package. We illustrate this using a simple \proglang {C++} function
97
76
which will be called by an \proglang {R} function.
@@ -107,12 +86,12 @@ in such add-on packages.
107
86
108
87
\subsection {Overview }
109
88
110
- \pkg {Rcpp} provides a function \Sexpr{link( " Rcpp.package.skeleton" ) } , modeled
111
- after the base \proglang {R} function \Sexpr{link( " package.skeleton" ) } , which
89
+ \pkg {Rcpp} provides a function \rdoc { Rcpp}{Rcpp .package.skeleton}, modeled
90
+ after the base \proglang {R} function \rdoc {utils}{ package.skeleton}, which
112
91
facilitates creation of a skeleton package using \pkg {Rcpp}.
113
92
114
- \Sexpr{link( " Rcpp.package.skeleton" ) } has a number of arguments documented on
115
- its help page (and similar to those of \Sexpr{link( " package.skeleton" ) } ). The
93
+ \rdoc { Rcpp}{Rcpp .package.skeleton} has a number of arguments documented on
94
+ its help page (and similar to those of \rdoc {utils}{ package.skeleton}). The
116
95
main argument is the first one which provides the name of the package one
117
96
aims to create by invoking the function. An illustration of a call using an
118
97
argument \texttt {mypackage } is provided below.
@@ -138,15 +117,15 @@ mypackage-package.Rd
138
117
rcpp_hello_world.Rd
139
118
140
119
mypackage/src:
141
- Makevars
142
- Makevars.win
120
+ Makevars ## up until Rcpp 0.10.6, see below
121
+ Makevars.win ## up until Rcpp 0.10.6, see below
143
122
RcppExports.cpp
144
123
rcpp_hello_world.cpp
145
124
$
146
125
\end {verbatim }
147
126
\end {Hchunk }
148
127
149
- Using \Sexpr{link( " Rcpp.package.skeleton" ) } is by far the simplest approach
128
+ Using \rdoc { Rcpp}{Rcpp .package.skeleton} is by far the simplest approach
150
129
as it fulfills two roles. It creates the complete set of files needed for a
151
130
package, and it also includes the different components needed for using
152
131
\pkg {Rcpp} that we discuss in the following sections.
@@ -182,7 +161,7 @@ This function is preceded by the \texttt{Rcpp::export} attribute to automaticall
182
161
handle argument conversion because \proglang {R} has to be taught how to
183
162
e.g. handle the \texttt {List } class.
184
163
185
- \Sexpr{link( " Rcpp.package.skeleton" ) } then invokes \Sexpr{link( " compileAttributes" ) }
164
+ \rdoc { Rcpp}{Rcpp .package.skeleton} then invokes \rdoc {Rcpp}{ compileAttributes}
186
165
on the package, which generates the \texttt {RcppExports.cpp } file:
187
166
188
167
<<lang =cpp >>=
@@ -210,15 +189,15 @@ END_RCPP
210
189
@
211
190
212
191
This file defines a function with the appropriate calling convention, suitable for
213
- \Sexpr{link( " .Call" ) } . It needs to be regenerated each time functions
192
+ \rdoc {base}{ .Call}. It needs to be regenerated each time functions
214
193
exposed by attributes are modified. This is the task of the
215
- \Sexpr{link( " compileAttributes" ) } function. A discussion on attributes is
194
+ \rdoc {Rcpp}{ compileAttributes} function. A discussion on attributes is
216
195
beyond the scope of this document and more information is available
217
196
in the attributes vignette \citep {CRAN:Rcpp:Attributes }.
218
197
219
198
\subsection {\proglang {R} code }
220
199
221
- The \Sexpr{link( " compileAttributes" ) } also generates \proglang {R} code
200
+ The \rdoc {Rcpp}{ compileAttributes} also generates \proglang {R} code
222
201
that uses the \proglang {C++} function.
223
202
224
203
<<lang =cpp >>=
@@ -231,12 +210,12 @@ rcpp_hello_world <- function() {
231
210
@
232
211
233
212
This is also a generated file so it should not be modified manually, rather
234
- regenerated as needed by \Sexpr{link( " compileAttributes" ) } .
213
+ regenerated as needed by \rdoc {Rcpp}{ compileAttributes}.
235
214
236
215
\subsection {\texttt {DESCRIPTION } }
237
216
238
217
The skeleton generates an appropriate \texttt {DESCRIPTION } file, using
239
- both \texttt {Depends : } and \texttt {LinkingTo } for \pkg {Rcpp}:
218
+ both \texttt {Imports : } and \texttt {LinkingTo } for \pkg {Rcpp}:
240
219
241
220
\begin {Hchunk }
242
221
\begin {verbatim }
@@ -249,27 +228,37 @@ Author: Who wrote it
249
228
Maintainer: Who to complain to <[email protected] >
250
229
Description: More about what it does (maybe more than one line)
251
230
License: What Licence is it under ?
252
- Depends : Rcpp (>= 0.10.4.5 )
231
+ Imports : Rcpp (>= 0.11.0 )
253
232
LinkingTo: Rcpp
254
233
\end {verbatim }
255
234
\end {Hchunk }
256
235
257
- \Sexpr{link( " Rcpp.package.skeleton" ) } adds the three last lines to the
258
- \texttt {DESCRIPTION } file generated by \Sexpr{link( " package.skeleton" ) } .
236
+ \rdoc { Rcpp}{Rcpp .package.skeleton} adds the three last lines to the
237
+ \texttt {DESCRIPTION } file generated by \rdoc {utils}{ package.skeleton}.
259
238
260
- The \texttt {Depends } declaration indicates \proglang {R}-level dependency
261
- between the client package and \pkg {Rcpp}. The \texttt {LinkingTo } declaration
239
+ The \texttt {Imports } declaration indicates \proglang {R}-level dependency
240
+ between the client package and \pkg {Rcpp}; code from the latter is being
241
+ imported into the package described here. The \texttt {LinkingTo } declaration
262
242
indicates that the client package needs to use header files exposed by
263
243
\pkg {Rcpp}.
264
244
265
- \subsection {\texttt {Makevars } and \texttt {Makevars.win } }
245
+ \subsection {Now optional: \texttt {Makevars } and \texttt {Makevars.win } }
246
+
247
+ This behaviour changed with \pkg {Rcpp} release 0.11.0. These files used to be
248
+ mandatory, now they are merely optional.
249
+
250
+ We will describe the old setting first as it was in use for a few years. The
251
+ new standard, however, is much easier and is described below.
252
+
253
+ \subsubsection {Releases up until 0.10.6 }
266
254
267
- Unfortunately, the \texttt {LinkingTo } declaration in itself is not
255
+ Unfortunately, the \texttt {LinkingTo } declaration in itself was not
268
256
enough to link to the user \proglang {C++} library of \pkg {Rcpp}. Until more
269
- explicit support for libraries is added to \proglang {R}, we need to manually
257
+ explicit support for libraries is added to \proglang {R}, ones needes to manually
270
258
add the \pkg {Rcpp} library to the \texttt {PKG\_ LIBS } variable in the
271
- \texttt {Makevars } and \texttt {Makevars.win } files. \pkg {Rcpp} provides the
272
- unexported function \texttt {Rcpp:::LdFlags() } to ease the process:
259
+ \texttt {Makevars } and \texttt {Makevars.win } files. (This has now changed with
260
+ release 0.11.0; see below).
261
+ \pkg {Rcpp} provides the unexported function \texttt {Rcpp:::LdFlags() } to ease the process:
273
262
274
263
\begin {Hchunk }
275
264
\begin {verbatim }
@@ -311,24 +300,42 @@ PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()
311
300
\end {verbatim }
312
301
\end {Hchunk }
313
302
303
+ \subsubsection {Releases since 0.11.0 }
304
+
305
+ As of release 0.11.0, this is no longer needed as client packages obtain the
306
+ required code from \pkg {Rcpp} via explicit function registration. The user
307
+ does not have to do anything.
308
+
309
+ This means that \code {PKG\_ LIBS} can now be empty---unless some client
310
+ libraries are needed. For example, \pkg {RcppCNPy} needs compression support
311
+ and hence uses \code {PKG\_ LIBS= -lz}. Similarly, when a third-party library is
312
+ required, it can and should be set here.
313
+
314
314
\subsection {\texttt {NAMESPACE } }
315
315
316
- The \Sexpr{link( " Rcpp.package.skeleton" ) } function also creates a file
316
+ The \rdoc { Rcpp}{Rcpp .package.skeleton} function also creates a file
317
317
\texttt {NAMESPACE }.
318
318
319
319
\begin {Hchunk }
320
320
\begin {verbatim }
321
321
useDynLib(mypackage)
322
322
exportPattern("^[[:alpha:]]+")
323
+ importFrom{Rcpp, evalCpp}
323
324
\end {verbatim }
324
325
\end {Hchunk }
325
326
326
- This file serves two purposes. First, it ensure that the dynamic library
327
+ This file serves three purposes. First, it ensure that the dynamic library
327
328
contained in the package we are creating via
328
- \Sexpr{link(" Rcpp.package.skeleton" )} will be loaded and thereby made
329
- available to the newly created \proglang {R} package. Second, it declares
330
- which functions should be globally visible from the namespace of this
331
- package. As a reasonable default, we export all functions.
329
+ \rdoc {Rcpp}{Rcpp.package.skeleton} will be loaded and thereby made
330
+ available to the newly created \proglang {R} package.
331
+
332
+ Second, it declares which functions should be globally visible from the
333
+ namespace of this package. As a reasonable default, we export all functions.
334
+
335
+ Third, it instructs R to import a symbol from \pkg {Rcpp}. This sets up the
336
+ import of all registered function and, together with the \code {Imports:}
337
+ statement in \code {DESCRIPTION}, provides what is needed for client packages
338
+ to access \pkg {Rcpp} functionality.
332
339
333
340
\subsection {Help files }
334
341
@@ -417,7 +424,7 @@ rcpp_hello_world()
417
424
\section {Using modules }
418
425
419
426
This document does not cover the use of the \texttt {module } argument
420
- of \Sexpr{link( " Rcpp.package.skeleton" ) } . It is covered
427
+ of \rdoc { Rcpp}{Rcpp .package.skeleton}. It is covered
421
428
in the modules vignette \citep {CRAN:Rcpp:Modules }.
422
429
423
430
\section {Further examples }
@@ -455,7 +462,7 @@ manual~\citep{R:Extensions} for details.
455
462
456
463
This document described how to use the \pkg {Rcpp} package for \proglang {R}
457
464
and \proglang {C++} integration when writing an \proglang {R} extension
458
- package. The use of the \Sexpr{link( " Rcpp.package.skeleton" ) } was shown in
465
+ package. The use of the \rdoc { Rcpp}{Rcpp .package.skeleton} was shown in
459
466
detail, and references to further examples were provided.
460
467
461
468
\bibliographystyle {plainnat}
0 commit comments