2525
2626\newcommand {\proglang }[1]{\textsf {#1}}
2727\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}}}
2830
2931<<version ,echo =FALSE ,print =FALSE >>=
3032prettyVersion <- packageDescription(" Rcpp" )$ Version
3133prettyDate <- format(Sys.Date(), " %B %e, %Y" )
34+ require(Rcpp )
35+ require(highlight )
3236@
3337
3438\author {Dirk Eddelbuettel \and Romain Fran\c {c}ois}
3539\title {Writing a package that uses \pkg {Rcpp} }
3640\date {\pkg {Rcpp} version \Sexpr{prettyVersion } as of \Sexpr{prettyDate } }
3741
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-
6242
6343\begin {document }
6444\maketitle
6545
6646\abstract {
6747 \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. }
7756
7857\section {Introduction }
7958
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}.
8665
8766It should be noted that \proglang {R} has only limited support for
8867\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
9170\pkg {Rcpp}), but support for linking against a library is not provided by
9271\proglang {R} and has to be added manually.
9372
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}
9574function to illustrate a recommended way of using \pkg {Rcpp} from a client
9675package. We illustrate this using a simple \proglang {C++} function
9776which will be called by an \proglang {R} function.
@@ -107,12 +86,12 @@ in such add-on packages.
10786
10887\subsection {Overview }
10988
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
11291facilitates creation of a skeleton package using \pkg {Rcpp}.
11392
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
11695main argument is the first one which provides the name of the package one
11796aims to create by invoking the function. An illustration of a call using an
11897argument \texttt {mypackage } is provided below.
@@ -138,15 +117,15 @@ mypackage-package.Rd
138117rcpp_hello_world.Rd
139118
140119mypackage/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
143122RcppExports.cpp
144123rcpp_hello_world.cpp
145124$
146125\end {verbatim }
147126\end {Hchunk }
148127
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
150129as it fulfills two roles. It creates the complete set of files needed for a
151130package, and it also includes the different components needed for using
152131\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
182161handle argument conversion because \proglang {R} has to be taught how to
183162e.g. handle the \texttt {List } class.
184163
185- \Sexpr{link( " Rcpp.package.skeleton" ) } then invokes \Sexpr{link( " compileAttributes" ) }
164+ \rdoc { Rcpp}{Rcpp .package.skeleton} then invokes \rdoc {Rcpp}{ compileAttributes}
186165on the package, which generates the \texttt {RcppExports.cpp } file:
187166
188167<<lang =cpp >>=
@@ -210,15 +189,15 @@ END_RCPP
210189@
211190
212191This 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
214193exposed 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
216195beyond the scope of this document and more information is available
217196in the attributes vignette \citep {CRAN:Rcpp:Attributes }.
218197
219198\subsection {\proglang {R} code }
220199
221- The \Sexpr{link( " compileAttributes" ) } also generates \proglang {R} code
200+ The \rdoc {Rcpp}{ compileAttributes} also generates \proglang {R} code
222201that uses the \proglang {C++} function.
223202
224203<<lang =cpp >>=
@@ -231,12 +210,12 @@ rcpp_hello_world <- function() {
231210@
232211
233212This 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}.
235214
236215\subsection {\texttt {DESCRIPTION } }
237216
238217The 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}:
240219
241220\begin {Hchunk }
242221\begin {verbatim }
@@ -249,27 +228,37 @@ Author: Who wrote it
249228Maintainer: Who to complain to <yourfault@somewhere.net>
250229Description: More about what it does (maybe more than one line)
251230License: What Licence is it under ?
252- Depends : Rcpp (>= 0.10.4.5 )
231+ Imports : Rcpp (>= 0.11.0 )
253232LinkingTo: Rcpp
254233\end {verbatim }
255234\end {Hchunk }
256235
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}.
259238
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
262242indicates that the client package needs to use header files exposed by
263243\pkg {Rcpp}.
264244
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 }
266254
267- Unfortunately, the \texttt {LinkingTo } declaration in itself is not
255+ Unfortunately, the \texttt {LinkingTo } declaration in itself was not
268256enough 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
270258add 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:
273262
274263\begin {Hchunk }
275264\begin {verbatim }
@@ -311,24 +300,42 @@ PKG_LIBS = $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()
311300\end {verbatim }
312301\end {Hchunk }
313302
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+
314314\subsection {\texttt {NAMESPACE } }
315315
316- The \Sexpr{link( " Rcpp.package.skeleton" ) } function also creates a file
316+ The \rdoc { Rcpp}{Rcpp .package.skeleton} function also creates a file
317317\texttt {NAMESPACE }.
318318
319319\begin {Hchunk }
320320\begin {verbatim }
321321useDynLib(mypackage)
322322exportPattern("^[[:alpha:]]+")
323+ importFrom{Rcpp, evalCpp}
323324\end {verbatim }
324325\end {Hchunk }
325326
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
327328contained 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.
332339
333340\subsection {Help files }
334341
@@ -417,7 +424,7 @@ rcpp_hello_world()
417424\section {Using modules }
418425
419426This 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
421428in the modules vignette \citep {CRAN:Rcpp:Modules }.
422429
423430\section {Further examples }
@@ -455,7 +462,7 @@ manual~\citep{R:Extensions} for details.
455462
456463This document described how to use the \pkg {Rcpp} package for \proglang {R}
457464and \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
459466detail, and references to further examples were provided.
460467
461468\bibliographystyle {plainnat}
0 commit comments