RCPP FAQ: Dirk Eddelbuettel and Romain François
RCPP FAQ: Dirk Eddelbuettel and Romain François
This document attempts to answer the most Frequently Asked Questions 3.6 How do I write a plugin for inline and/or Rcpp
(FAQ) regarding the Rcpp (Eddelbuettel et al., 2019a; Eddelbuettel and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 7
François, 2011; Eddelbuettel, 2013) package. 3.7 How can I pass one additional flag to the compiler 7
3.8 How can I set matrix row and column names . . . . 8
Rcpp | FAQ | R | C++
3.9 Why can long long types not be cast correctly . . . . 8
3.10 What LaTeX packages do I need to typeset the vignettes 8
Contents 3.11 Why is there a limit of 20 on some constructors . . 8
3.12 Can I use default function parameters with Rcpp . 9
1 Getting started 1 3.13 Can I use C++11, C++14, C++17, . . . with Rcpp . 9
1.1 How do I get started . . . . . . . . . . . . . . . . . . . 1 3.14 How do I use it within (Python’s) Conda setup? . . 9
1.2 What do I need . . . . . . . . . . . . . . . . . . . . . . 2
1.3 What compiler can I use . . . . . . . . . . . . . . . . . 2 4 Support 9
1.4 What other packages are useful . . . . . . . . . . . . 2 4.1 Is the API documented . . . . . . . . . . . . . . . . . . 9
1.5 What licenses can I choose for my code . . . . . . . . 2 4.2 Does it really work . . . . . . . . . . . . . . . . . . . . 9
4.3 Where can I ask further questions . . . . . . . . . . . 9
2 Compiling and Linking 2
4.4 Where can I read old questions and answers . . . . 9
2.1 How do I use Rcpp in my package . . . . . . . . . . . 2
4.5 I like it. How can I help . . . . . . . . . . . . . . . . . 10
2.2 How do I quickly prototype my code . . . . . . . . . 2
2.2.1 Using inline . . . . . . . . . . . . . . . . . . . . 2 4.6 I don’t like it. How can I help . . . . . . . . . . . . . . 10
2.2.2 Using Rcpp Attributes . . . . . . . . . . . . . . 3 4.7 Can I have commercial support for Rcpp . . . . . . . 10
2.3 How do I convert my prototyped code to a package 3 4.8 I want to learn quickly. Do you provide training
2.4 How do I quickly prototype my code in a package . 3 courses . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 But I want to compile my code with R CMD SHLIB 3 4.9 Where is the code repository . . . . . . . . . . . . . . 10
2.6 But R CMD SHLIB still does not work . . . . . . . . . 4
5 Known Issues 10
2.7 What about LinkingTo . . . . . . . . . . . . . . . . . 4
2.8 Does Rcpp work on windows . . . . . . . . . . . . . . 4 5.1 Rcpp changed the (const) object I passed by value . 10
2.9 Can I use Rcpp with Visual Studio . . . . . . . . . . . 4 5.2 Issues with implicit conversion from an Rcpp object
2.10 I am having problems building Rcpp on macOS, any to a scalar or other Rcpp object . . . . . . . . . . . . 11
help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5.3 Using operator= with a scalar replaced the object
2.10.1 Lack of a Compiler . . . . . . . . . . . . . . . . 4 instead of filling element-wise . . . . . . . . . . . . . 11
2.10.2 Differing macOS R Versions Leading to Bi- 5.4 Long Vector support on Windows . . . . . . . . . . . 12
nary Failures . . . . . . . . . . . . . . . . . . . 4 5.5 Sorting with STL on a CharacterVector produces
2.10.3 OpenMP Support . . . . . . . . . . . . . . . . . 4 problematic results . . . . . . . . . . . . . . . . . . . . 12
2.10.4 Additional Information and Help . . . . . . . 4 5.6 Lexicographic order of string sorting differs due to
2.11 Does Rcpp work on solaris/suncc . . . . . . . . . . . 4 capitalization . . . . . . . . . . . . . . . . . . . . . . . . 13
2.12 Does Rcpp work with Revolution R . . . . . . . . . . 4 5.7 Package building fails with ‘symbols not found’ . . . 13
2.13 Is it related to Rho (formerly CXXR) . . . . . . . . . 4
2.14 How do I quickly prototype my code using Attributes 5 1. Getting started
2.15 What about the new ‘no-linking’ feature . . . . . . . 5
2.16 I am having problems building RcppArmadillo on 1.1. How do I get started. If you have Rcpp installed, please exe-
macOS, any help . . . . . . . . . . . . . . . . . . . . . 5 cute the following command in R to access the introductory vignette
(which is a variant of the Eddelbuettel and François (2011) and
3 Examples 5 Eddelbuettel and Balamuta (2017, 2018) papers) for a detailed in-
3.1 Can I use templates with Rcpp . . . . . . . . . . . . . 5 troduction, ideally followed by at least the Rcpp Attributes (Allaire
3.1.1 Using inline with Templated Code . . . . . . 5 et al., 2018) vignette:
3.1.2 Using Rcpp Attributes with Templated Code 5
3.2 Can I do matrix algebra with Rcpp . . . . . . . . . . 5 vignette("Rcpp-jss-2011")
3.2.1 Using inline with RcppArmadillo . . . . . . . 6 vignette("Rcpp-introduction")
3.2.2 Using Rcpp Attributes with RcppArmadillo . 6 vignette("Rcpp-attributes")
3.3 Can I use code from the Rmath header and library
with Rcpp . . . . . . . . . . . . . . . . . . . . . . . . . . 6 If you do not have Rcpp installed, these documents should also
3.4 Can I use NA and Inf with Rcpp . . . . . . . . . . . . 7 be available whereever you found this document, i.e., on every
3.5 Can I easily multiply matrices . . . . . . . . . . . . . 7 mirror site of CRAN.
• a development environment with a suitable compiler (see rbenchmark to run simple timing comparisons and benchmarks;
below), header files and required libraries; it is also recommended but not required.
• R should be built in a way that permits linking and pos-
microbenchmark is an alternative for benchmarking.
sibly embedding of R; this is typically ensured by the
--enable-shared-lib option; devtools can help the process of building, compiling and testing
• standard development tools such as make etc. a package but it too is entirely optional.
Also see the RStudio documentation on pre-requisites for R
1.5. What licenses can I choose for my code. The Rcpp package
package development.
is licensed under the terms of the GNU GPL 2 or later, just like R
itself. A key goal of the Rcpp package is to make extending R more
1.3. What compiler can I use. On almost all platforms, the GNU
seamless. But by linking your code against R (as well as Rcpp),
Compiler Collection (or gcc, which is also the name of its C lan-
the combination is bound by the GPL as well. This is very clearly
guage compiler) has to be used along with the corresponding g++
stated at the FSF website:
compiler for the C++ language. A minimal suitable version is a final
4.2.* release; earlier 4.2.* were lacking some C++ features (and Linking a GPL covered work statically or dynamically
even 4.2.1, still used on OS X as the last gcc release), has issues). with other modules is making a combined work based
Generally speaking, the default compilers on all the common on the GPL covered work. Thus, the terms and condi-
platforms are suitable. tions of the GNU General Public License cover the whole
Specific per-platform notes: combination.
Windows users need the Rtools package from the site maintained So you are free to license your work under whichever terms
by Duncan Murdoch which contains all the required tools in you find suitable (provided they are GPL-compatible, see the FSF
a single package; complete instructions specific to Windows site for details). However, the combined work will remain under
are in the ‘R Administration’ manual (R Core Team, 2018a, the terms and conditions of the GNU General Public License. This
Appendix D). As of August 2014, it still installs the gcc/g++ restriction comes from both R which is GPL-licensed as well as from
4.6.* compiler which limits the ability to use modern C++ Rcpp and whichever other GPL-licensed components you may be
standards so only s-std=c++0x is supported. R 3.1.0 and linking against.
above detect this and set appropriate flags.
The clang and clang++ compilers from the LLVM project can • The older one is provided by the inline package and described
also be used. On Linux, they are inter-operable with gcc et al. On in Section~2.2.1.
OS X, they are unfortunately not ABI compatible. The clang++ • Starting with Rcpp 0.10.0, the Rcpp Attributes feature (de-
compiler is interesting as it emits much more comprehensible error scribed in Section~2.2.2) offered an even easier alternative
messages than g++ (though g++ 4.8 and 4.9 have caught up). via the function evalCpp, cppFunction and sourceCpp.
The Intel icc family has also been used successfully as its output
files can also be combined with those from gcc. The next two subsections show an example each.
2.2.1. Using inline. The inline package (Sklyar et al., 2018) provides
1.4. What other packages are useful. Additional packages that we
the functions cfunction and cxxfunction. Below is a simple
have found useful are:
function that uses accumulate from the (C++) Standard Template
inline which is invaluable for direct compilation, linking and load- Library to sum the elements of a numeric vector.
ing of short code snippets—but now effectively superseded
2.6. But R CMD SHLIB still does not work. We have had reports 2.10.3. OpenMP Support. By default, the macOS operating environ-
in the past where build failures occurred when users had non- ment lacks the ability to parallelize sections of code using the
standard code in their ~/.Rprofile or Rprofile.site (or equiv- [OpenMP](http://openmp.org/wp/) standard. Within R 3.4.*, the de-
alent) files. fault developer environment was changed to allow for OpenMP to
If such code emits text on stdout, the frequent and implicit be used on macOS by using a non-default toolchain provided by
invocation of Rscript -e "..." (as in FAQ 2.5 above) to retrieve R Core Team maintainers for macOS. Having said this, it is still
settings directly from Rcpp will fail. important to protect any reference to OpenMP as some users may
You may need to uncomment such non-standard code, or protect not yet have the ability to use OpenMP.
it by wrapping it inside if (interactive()), or possibly try to To setup the appropriate protection for using OpenMP, the pro-
use Rscript --vanilla instead of plain Rscript. cess is two-fold. First, protect the inclusion of headers with:
#ifdef _OPENMP
2.7. What about LinkingTo. R has only limited support for cross-
#include <omp.h>
package linkage.
#endif
We now employ the LinkingTo field of the DESCRIPTION file
of packages using Rcpp. But this only helps in having R compute
the location of the header files for us. Second, when parallelizing portions of code use:
The actual library location and argument still needs to be pro-
#ifdef _OPENMP
vided by the user. How to do so has been shown above, and we
// multithreaded OpenMP version of code
recommned you use either FAQ 2.1 or FAQ 2.2.1 both which use
#else
the Rcpp function Rcpp:::LdFlags().
// single-threaded version of code
If and when LinkingTo changes and lives up to its name, we
#endif
will be sure to adapt Rcpp as well.
An important change arrive with Rcpp release 0.11.0 and con-
cern the automatic registration of functions; see Section~2.15 Under this approach, the code will be safely parallelized when
below. support exists for OpenMP on Windows, macOS, and Linux.
// [[Rcpp::export]] 4.1. Is the API documented. You bet. We use doxygen to generate
void sample_defaults( html, latex and man page documentation from the source. The
NumericVector x = html documentation is available for browsing, as a very large pdf
NumericVector::create(), // Size 0 vector file, and all three formats are also available a zip-archives: html,
bool bias = true, // Set to true latex, and man.
std::string method =
"rcpp rules!") { // Set string 4.2. Does it really work. We take quality seriously and have devel-
opped an extensive unit test suite to cover many possible uses of
Rcpp::Rcout << "x size: " << x.size() << ", "; the Rcpp API.
Rcpp::Rcout << "bias value: " << bias << ", "; We are always on the look for more coverage in our testing.
Rcpp::Rcout << "method value: " << "."; Please let us know if something has not been tested enough.
}
4.3. Where can I ask further questions. The Rcpp-devel mailing
/*** R list hosted at R-forge is by far the best place. You may also want
sample_defaults() # all defaults to look at the list archives to see if your question has been asked
sample_defaults(1:5) # supply x values before.
You can also use StackOverflow via its ‘rcpp’ tag.
sample_defaults(bias = FALSE, # supply bool
method = "Rlang") # and string 4.4. Where can I read old questions and answers. The normal
*/ Rcpp-devel mailing list hosting at R-forge contains an archive,
which can be searched via swish.
Note: In cpp, the default bool values are true and false Alternatively, one can also use Mail-Archive on Rcpp-devel
whereas in R the valid types are TRUE or FALSE. which offers web-based interfaces, including searching.
4.7. Can I have commercial support for Rcpp. Sure you can. Just R use
send us an email, and we will be happy to discuss the request.
a <- 1.5:4.5
4.8. I want to learn quickly. Do you provide training courses. Yes. b <- 1.5:4.5
Just send us an email. implicit_ref(a)
a
4.9. Where is the code repository. From late 2008 to late 2013, we # [1] 2.5 3.5 4.5 5.5
used the Subversion repository at R-Forge which contained Rcpp explicit_ref(b)
and a number of related packages. It still has the full history as b
well as number of support files. # [1] 2.5 3.5 4.5 5.5
We have since switched to a Git repository at Github for Rcpp
(as well as for RcppArmadillo and RcppEigen). There are two exceptions to this rule. The first exception is
that a deep copy of the object can be made by explicit use of
Rcpp:clone(). In this case, the cloned object has no link to the
5. Known Issues
original R object. However, there is a time cost associated with
Contained within this section is a list of known issues regarding this procedure as new memory must be allocated and the previous
Rcpp. The issues listed here are either not able to be fixed due values must be copied over. The second exception, which was
to breaking application binary interface (ABI), would impact the previously foreshadowed, is encountered when Rcpp and R object
ability to reproduce pre-existing results, or require significant work. types do not match. One frequent example of this case is when the
Generally speaking, these issues come to light only when pushing R object generated from seq() or a:b reports a class of "integer"
the edge capabilities of Rcpp. while the Rcpp object is setup to receive the class of "numeric"
as its object is set to NumericVector or NumericMatrix. In such
5.1. Rcpp changed the (const) object I passed by value. Rcpp ob- cases, this would lead to a new SEXP object being created behind the
jects are wrappers around the underlying R objects’ SEXP, or S- scenes and, thus, there would not be a link between the Rcpp object
expression. The SEXP is a pointer variable that holds the location and R object. So, any changes in C++ would not be propagated to
of where the R object data has been stored (R Core Team, 2018b, R unless otherwise specified.
Section 1.1). That is to say, the SEXP does not hold the actual data
#include <Rcpp.h>
of the R object but merely a reference to where the data resides.
When creating a new Rcpp object for an R object to enter C++, this
// [[Rcpp::export]]
object will use the same SEXP that powers the original R object
void int_vec_type(Rcpp::IntegerVector X) {
if the types match otherwise a new SEXP must be created to be
X = X + 1.0;
type safe. In essence, the underlying SEXP objects are passed by
}
reference without explicit copies being made into C++. We refer to
this arrangement as a proxy model.
// [[Rcpp::export]]
As for the actual implementation, there are a few conse-
void num_vec_type(Rcpp::NumericVector X) {
quences of the proxy model. The foremost consequence within
X = X + 1.0;
this paradigm is that pass by value is really a pass by reference.
}
In essence, the distinction between the following two functions is
only visual sugar:
R use:
void implicit_ref(NumericVector X); a <- 1:5
void explicit_ref(NumericVector& X); b <- 1:5
class(a)
In particular, when one is passing by value what occurs is the # [1] "integer"
instantiation of the new Rcpp object that uses the same SEXP for int_vec_type(a)
the R object. As a result, the Rcpp object is “linked” to the original