@@ -67,7 +67,9 @@ footer_contents: "Rcpp Vignette"
6767skip_final_break : true
6868
6969# Produce a pinp document
70- output : pinp::pinp
70+ output :
71+ pinp::pinp :
72+ collapse : true
7173
7274header-includes : >
7375 \newcommand{\proglang}[1]{\textsf{#1}}
@@ -85,6 +87,13 @@ vignette: >
8587
8688\tableofcontents
8789
90+ ``` {r setup, include=FALSE}
91+ knitr::opts_chunk$set(cache=TRUE)
92+ library(Rcpp)
93+ library(inline)
94+ options("width"=50, digits=5)
95+ ```
96+
8897# Getting started
8998
9099## How do I get started
@@ -246,7 +255,7 @@ The \pkg{inline} package \citep{CRAN:inline} provides the functions
246255function that uses ` accumulate ` from the (\proglang{C++}) Standard
247256Template Library to sum the elements of a numeric vector.
248257
249- ``` {r, eval = FALSE }
258+ ``` {r}
250259fx <- cxxfunction(signature(x = "numeric"),
251260 'NumericVector xx(x);
252261 return wrap(
@@ -259,18 +268,6 @@ res <- fx(seq(1, 10, by=0.5))
259268res
260269```
261270
262- ``` {r, eval = FALSE, echo=FALSE}
263- stopifnot(identical(res, sum(seq(1, 10, by=0.5))))
264- ```
265-
266- <!--
267- \pkg{Rcpp} uses \pkg{inline} to power its entire unit test suite. Consult the
268- `unitTests` directory of \pkg{Rcpp} for several hundred further examples.
269- ```{r, eval = FALSE}
270- list.files( system.file( "unitTests", package = "Rcpp" ), pattern = "^runit[.]" )
271- ```
272- -->
273-
274271One might want to use code that lives in a \proglang{C++} file instead of writing
275272the code in a character string in R. This is easily achieved by using
276273\rdoc{base}{readLines}:
@@ -289,16 +286,17 @@ useful as it shows how \pkg{inline} runs the show.
289286
290287Rcpp Attributes \citep{CRAN:Rcpp: Attributes }, and also discussed in
291288\faq{prototype-using-attributes} below, permits an even easier
292- route to integrating R and C++. It provides three key functions. First, \rdoc{Rcpp}{evalCpp}
293- provide a means to evaluate simple C++ expression which is often useful for
294- small tests, or to simply check if the toolchain is set up
295- correctly. Second, \rdoc{Rcpp}{cppFunction} can be used to create C++ functions
296- for R use on the fly. Third, ` Rcpp::sourceCpp ` can integrate entire files in
297- order to define multiple functions.
289+ route to integrating R and C++. It provides three key functions.
290+ First, \rdoc{Rcpp}{evalCpp} provide a means to evaluate simple C++
291+ expression which is often useful for small tests, or to simply check
292+ if the toolchain is set up correctly. Second, \rdoc{Rcpp}{cppFunction}
293+ can be used to create C++ functions for R use on the fly. Third,
294+ ` Rcpp::sourceCpp ` can integrate entire files in order to define
295+ multiple functions.
298296
299297The example above can now be rewritten as:
300298
301- ``` {r, eval = FALSE }
299+ ``` {r}
302300cppFunction('double accu(NumericVector x) {
303301 return(
304302 std::accumulate(x.begin(), x.end(), 0.0)
@@ -647,7 +645,7 @@ what we show below.
647645Most certainly, consider this simple example of a templated class
648646which squares its argument:
649647
650- ``` {r, eval = FALSE }
648+ ``` {r}
651649inc <- 'template <typename T>
652650 class square :
653651 public std::unary_function<T,T> {
@@ -821,7 +819,7 @@ two calls result in the same random draws. If we wanted to control the draws,
821819we could explicitly set the seed after the `RNGScope` object has been
822820instantiated.
823821
824- ```{r, eval = FALSE }
822+ ```{r}
825823fx <- cxxfunction(signature(),
826824 'RNGScope();
827825 return rnorm(5, 0, 100);',
@@ -837,7 +835,7 @@ random variable distributed as $N(m,s)$.
837835
838836Using Rcpp Attributes, this can be as simple as
839837
840- ``` {r, eval = FALSE }
838+ ``` {r}
841839cppFunction('Rcpp::NumericVector ff(int n) {
842840 return rnorm(n, 0, 100); }')
843841set.seed(42)
@@ -858,7 +856,7 @@ as well as R, and that identical random number draws are obtained.
858856
859857\noindent Yes, see the following example:
860858
861- ``` {r, eval = FALSE }
859+ ``` {r}
862860src <- 'Rcpp::NumericVector v(4);
863861 v[0] = R_NegInf; // -Inf
864862 v[1] = NA_REAL; // NA
@@ -892,7 +890,7 @@ Rcpp::NumericVector fun(void) {
892890\noindent Yes, via the \pkg{RcppArmadillo} package which builds upon \pkg{Rcpp} and the
893891wonderful Armadillo library described above in \faq{matrix-algebra}:
894892
895- ```{r, eval = FALSE }
893+ ```{r}
896894txt <- 'arma::mat Am = Rcpp::as< arma::mat >(A);
897895 arma::mat Bm = Rcpp::as< arma::mat >(B);
898896 return Rcpp::wrap( Am * Bm );'
@@ -903,16 +901,9 @@ mmult <- cxxfunction(signature(A="numeric",
903901A <- matrix(1:9, 3, 3)
904902B <- matrix(9:1, 3, 3)
905903C <- mmult(A, B)
904+ C
906905```
907906
908- <!--
909- ```{r eval=FALSE}
910- A <- matrix(1:9, 3, 3)
911- B <- matrix(9:1, 3, 3)
912- A %*% B
913- ```
914- -->
915-
916907Armadillo supports a full range of common linear algebra operations.
917908
918909The \pkg{RcppEigen} package provides an alternative using the
@@ -1334,7 +1325,7 @@ As a result, the \pkg{Rcpp} object is ``linked'' to the original \proglang{R} ob
13341325Thus, if an operation is performed on the \pkg{Rcpp} object, such as adding 1
13351326to each element, the operation also updates the \proglang{R} object causing the change to be propagated to \proglang{R}'s interactive environment.
13361327
1337- ```cpp
1328+ ```{Rcpp}
13381329#include<Rcpp.h>
13391330
13401331// [[Rcpp::export]]
@@ -1348,9 +1339,9 @@ void explicit_ref(Rcpp::NumericVector& X) {
13481339}
13491340```
13501341
1351- R use:
1342+ R use
13521343
1353- ``` {r, eval = FALSE }
1344+ ``` {r}
13541345a <- 1.5:4.5
13551346b <- 1.5:4.5
13561347implicit_ref(a)
@@ -1374,7 +1365,7 @@ and, thus, there would _not_ be a link between the \pkg{Rcpp} object
13741365and \proglang{R} object. So, any changes in \proglang{C++} would not be propagated to
13751366\proglang{R} unless otherwise specified.
13761367
1377- ``` cpp
1368+ ``` {Rcpp}
13781369#include<Rcpp.h>
13791370
13801371// [[Rcpp::export]]
@@ -1390,14 +1381,14 @@ void num_vec_type(Rcpp::NumericVector X) {
13901381
13911382R use:
13921383
1393- ```{r, eval=FALSE }
1384+ ``` {r}
13941385a <- 1:5
13951386b <- 1:5
13961387class(a)
13971388int_vec_type(a)
1398- a
1389+ a # variable a changed as a side effect
13991390num_vec_type(b)
1400- b
1391+ b # b unchanged as copy was made for numeric
14011392```
14021393
14031394With this being said, there is one last area of contention with the proxy model:
@@ -1410,36 +1401,43 @@ would be allowed to be modified by the compiler and, thus, modifying the initial
14101401` SEXP ` object. Therefore, the initially secure \proglang{R} object would be altered.
14111402To illustrate this phenomenon, consider the following scenario:
14121403
1413- ``` cpp
1404+ ``` {Rcpp}
14141405#include <Rcpp.h>
14151406
14161407// [[Rcpp::export]]
1417- Rcpp::NumericVector const_override_ex (
1418- const Rcpp::NumericVector & X) {
1408+ Rcpp::IntegerVector const_override_ex(
1409+ const Rcpp::IntegerVector & X) {
14191410
1420- Rcpp::NumericVector Y(X); // Create object
1411+ Rcpp::IntegerVector Y(X); // Create object
14211412 // from SEXP
14221413
14231414 Y = Y * 2; // Modify new object
14241415
1425- return X ; // Return old object
1416+ return Y ; // Return new object
14261417}
14271418```
14281419
14291420R use:
14301421
1431- ```{r, eval=FALSE}
1432- x <- 1:10
1422+ ``` {r}
1423+ x <- 1:10 # an integer sequence
1424+ # returning an altered value
14331425const_override_ex(x)
1426+ # but the original value is altered too!
14341427x
14351428```
14361429
1430+ So we see that with ` SEXP ` objects, the ` const ` declaration can be
1431+ circumvented as it is really a pointer to the underlying R object.
1432+
1433+
14371434## Issues with implicit conversion from an \pkg{Rcpp} object to a scalar or other \pkg{Rcpp} object
14381435
1439- Not all \pkg{Rcpp} expressions are directly compatible with ` operator= ` .
1440- Compability issues stem from many \pkg{Rcpp} objects and functions returning an
1441- intermediary result which requires an explicit conversion. In such cases, the
1442- user may need to assist the compiler with the conversion.
1436+ Not all \pkg{Rcpp} expressions are directly compatible with
1437+ ` operator= ` . Compability issues stem from many \pkg{Rcpp} objects and
1438+ functions returning an intermediary result which requires an explicit
1439+ conversion. In such cases, the user may need to assist the compiler
1440+ with the conversion.
14431441
14441442There are two ways to assist with the conversion. The first is to construct
14451443storage variable for a result, calculate the result, and then store a value
@@ -1495,7 +1493,7 @@ is cast into the appropriate \pkg{Rcpp} object type. Therefore, if a
14951493via ` operator= ` , then the resulting ` Vector ` would have a length of
149614941 . See the following code snippet for the aforementioned behavior.
14971495
1498- ``` cpp
1496+ ``` {Rcpp}
14991497#include<Rcpp.h>
15001498
15011499// [[Rcpp::export]]
@@ -1515,7 +1513,7 @@ void vec_scalar_assign(int n, double fill_val) {
15151513
15161514R use:
15171515
1518- ```{r, eval=FALSE }
1516+ ``` {r}
15191517vec_scalar_assign(5L, 3.14)
15201518```
15211519
@@ -1526,7 +1524,7 @@ results while attempting to use the assignment operator with a scalar. In
15261524particular, the scalar will be coerced into a square ` Matrix ` and then
15271525assigned. For an example of this behavior, consider the following code:
15281526
1529- ``` cpp
1527+ ``` {Rcpp}
15301528#include<Rcpp.h>
15311529
15321530// [[Rcpp::export]]
@@ -1546,7 +1544,7 @@ void mat_scalar_assign(int n, double fill_val) {
15461544
15471545R use:
15481546
1549- ```{r, eval=FALSE }
1547+ ``` {r}
15501548mat_scalar_assign(2L, 3.0)
15511549```
15521550
@@ -1574,7 +1572,7 @@ by \pkg{Rcpp} attributes. This is engaged by adding
15741572For diagnostic and illustrativative purposes, consider the following code
15751573which checks to see if ` R_xlen_t ` is available on your platform:
15761574
1577- ``` cpp
1575+ ``` {Rcpp}
15781576#include <Rcpp.h>
15791577// Force compilation mode to C++11
15801578// [[Rcpp::plugins(cpp11)]]
@@ -1591,7 +1589,7 @@ bool test_long_vector_support() {
15911589
15921590R use:
15931591
1594- ``` {r, eval = FALSE }
1592+ ``` {r}
15951593test_long_vector_support()
15961594```
15971595
@@ -1627,7 +1625,7 @@ type. Though, sorting is slightly problematic due to locale as explained in the
16271625next entry. In the interim, the following code example illustrates the preferred
16281626approach alongside the problematic STL approach:
16291627
1630- ``` cpp
1628+ ``` {Rcpp}
16311629#include <Rcpp.h>
16321630
16331631// [[Rcpp::export]]
@@ -1653,7 +1651,7 @@ Rcpp::CharacterVector stl_sort(
16531651
16541652R use:
16551653
1656- ```{r, eval=FALSE }
1654+ ``` {r}
16571655set.seed(123)
16581656(X <- sample(c(LETTERS[1:5], letters[1:6]), 11))
16591657preferred_sort(X)
@@ -1680,7 +1678,7 @@ capitalized words are sorted together followed by the sorting of lowercase
16801678words instead of a mixture of capitalized and lowercase words. The issue is
16811679illustrated by the following code example:
16821680
1683- ``` cpp
1681+ ``` {Rcpp}
16841682#include <Rcpp.h>
16851683
16861684// [[Rcpp::export]]
@@ -1693,7 +1691,7 @@ Rcpp::CharacterVector rcpp_sort(
16931691
16941692R use:
16951693
1696- ```{r, eval=FALSE }
1694+ ``` {r}
16971695x <- c("B", "b", "c", "A", "a")
16981696sort(x)
16991697rcpp_sort(x)
0 commit comments