Skip to content

Commit 32272b8

Browse files
committed
updated vignettes, updated NEWS and ChangeLog
1 parent e150eba commit 32272b8

File tree

4 files changed

+107
-78
lines changed

4 files changed

+107
-78
lines changed

ChangeLog

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
2018-07-26 Dirk Eddelbuettel <[email protected]>
2+
3+
* vignettes/Rcpp-FAQ.Rmd: Use collapse: true
4+
5+
2018-07-25 Dirk Eddelbuettel <[email protected]>
6+
7+
* vignettes/Rcpp-extending.Rmd: Use collapse: true
8+
19
2018-07-24 Martin Lysy <[email protected]>
210

3-
* R/loadRcppClass.R: Search in R module for 'Class' instead of 'CppClass'.
11+
* R/loadRcppClass.R: Search in R module for 'Class' instead of
12+
'CppClass'.
413
* R/exposeClass.R: Fixed 'rename' argument to work as expected.
5-
* inst/unitTests/runit.exposeClass.R: Added unit tests for the above.
14+
* inst/unitTests/runit.exposeClass.R: Added unit tests for the
15+
above.
616

717
2018-07-23 Dirk Eddelbuettel <[email protected]>
818

inst/NEWS.Rd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
\newcommand{\ghpr}{\href{https://github.com/RcppCore/Rcpp/pull/#1}{##1}}
44
\newcommand{\ghit}{\href{https://github.com/RcppCore/Rcpp/issues/#1}{##1}}
55

6+
\section{Changes in Rcpp version 0.12.19 (2018-09-xx)}{
7+
\itemize{
8+
\item Changes in Rcpp API:
9+
\itemize{
10+
TBD
11+
}
12+
\item Changes in Rcpp Modules:
13+
\itemize{
14+
\item Improved \code{exposeClass} functionality along with added
15+
test (Martin Lysy in \ghpr{886} fixing \ghit{879}).
16+
}
17+
\item Changes in Rcpp Documentation:
18+
\itemize{
19+
\item Several vignettes now use the \code{collapse} argument to
20+
show output in the corresponding code block.
21+
}
22+
}
23+
}
24+
625
\section{Changes in Rcpp version 0.12.18 (2018-07-21)}{
726
\itemize{
827
\item Changes in Rcpp API:

vignettes/Rcpp-FAQ.Rmd

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ footer_contents: "Rcpp Vignette"
6767
skip_final_break: true
6868

6969
# Produce a pinp document
70-
output: pinp::pinp
70+
output:
71+
pinp::pinp:
72+
collapse: true
7173

7274
header-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
246255
function that uses `accumulate` from the (\proglang{C++}) Standard
247256
Template Library to sum the elements of a numeric vector.
248257

249-
```{r, eval = FALSE}
258+
```{r}
250259
fx <- cxxfunction(signature(x = "numeric"),
251260
'NumericVector xx(x);
252261
return wrap(
@@ -259,18 +268,6 @@ res <- fx(seq(1, 10, by=0.5))
259268
res
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-
274271
One might want to use code that lives in a \proglang{C++} file instead of writing
275272
the 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

290287
Rcpp 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

299297
The example above can now be rewritten as:
300298

301-
```{r, eval = FALSE}
299+
```{r}
302300
cppFunction('double accu(NumericVector x) {
303301
return(
304302
std::accumulate(x.begin(), x.end(), 0.0)
@@ -647,7 +645,7 @@ what we show below.
647645
Most certainly, consider this simple example of a templated class
648646
which squares its argument:
649647

650-
```{r, eval = FALSE}
648+
```{r}
651649
inc <- '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,
821819
we could explicitly set the seed after the `RNGScope` object has been
822820
instantiated.
823821
824-
```{r, eval = FALSE}
822+
```{r}
825823
fx <- cxxfunction(signature(),
826824
'RNGScope();
827825
return rnorm(5, 0, 100);',
@@ -837,7 +835,7 @@ random variable distributed as $N(m,s)$.
837835

838836
Using Rcpp Attributes, this can be as simple as
839837

840-
```{r, eval = FALSE}
838+
```{r}
841839
cppFunction('Rcpp::NumericVector ff(int n) {
842840
return rnorm(n, 0, 100); }')
843841
set.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}
862860
src <- '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
893891
wonderful Armadillo library described above in \faq{matrix-algebra}:
894892
895-
```{r, eval = FALSE}
893+
```{r}
896894
txt <- '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",
903901
A <- matrix(1:9, 3, 3)
904902
B <- matrix(9:1, 3, 3)
905903
C <- 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-
916907
Armadillo supports a full range of common linear algebra operations.
917908

918909
The \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
13341325
Thus, if an operation is performed on the \pkg{Rcpp} object, such as adding 1
13351326
to 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}
13541345
a <- 1.5:4.5
13551346
b <- 1.5:4.5
13561347
implicit_ref(a)
@@ -1374,7 +1365,7 @@ and, thus, there would _not_ be a link between the \pkg{Rcpp} object
13741365
and \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

13911382
R use:
13921383

1393-
```{r, eval=FALSE}
1384+
```{r}
13941385
a <- 1:5
13951386
b <- 1:5
13961387
class(a)
13971388
int_vec_type(a)
1398-
a
1389+
a # variable a changed as a side effect
13991390
num_vec_type(b)
1400-
b
1391+
b # b unchanged as copy was made for numeric
14011392
```
14021393

14031394
With 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.
14111402
To 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

14291420
R use:
14301421

1431-
```{r, eval=FALSE}
1432-
x <- 1:10
1422+
```{r}
1423+
x <- 1:10 # an integer sequence
1424+
# returning an altered value
14331425
const_override_ex(x)
1426+
# but the original value is altered too!
14341427
x
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

14441442
There are two ways to assist with the conversion. The first is to construct
14451443
storage 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
14951493
via `operator=`, then the resulting `Vector` would have a length of
14961494
1. 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

15161514
R use:
15171515

1518-
```{r, eval=FALSE}
1516+
```{r}
15191517
vec_scalar_assign(5L, 3.14)
15201518
```
15211519

@@ -1526,7 +1524,7 @@ results while attempting to use the assignment operator with a scalar. In
15261524
particular, the scalar will be coerced into a square `Matrix` and then
15271525
assigned. 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

15471545
R use:
15481546

1549-
```{r, eval=FALSE}
1547+
```{r}
15501548
mat_scalar_assign(2L, 3.0)
15511549
```
15521550

@@ -1574,7 +1572,7 @@ by \pkg{Rcpp} attributes. This is engaged by adding
15741572
For diagnostic and illustrativative purposes, consider the following code
15751573
which 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

15921590
R use:
15931591

1594-
```{r, eval = FALSE}
1592+
```{r}
15951593
test_long_vector_support()
15961594
```
15971595

@@ -1627,7 +1625,7 @@ type. Though, sorting is slightly problematic due to locale as explained in the
16271625
next entry. In the interim, the following code example illustrates the preferred
16281626
approach 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

16541652
R use:
16551653

1656-
```{r, eval=FALSE}
1654+
```{r}
16571655
set.seed(123)
16581656
(X <- sample(c(LETTERS[1:5], letters[1:6]), 11))
16591657
preferred_sort(X)
@@ -1680,7 +1678,7 @@ capitalized words are sorted together followed by the sorting of lowercase
16801678
words instead of a mixture of capitalized and lowercase words. The issue is
16811679
illustrated 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

16941692
R use:
16951693

1696-
```{r, eval=FALSE}
1694+
```{r}
16971695
x <- c("B", "b", "c", "A", "a")
16981696
sort(x)
16991697
rcpp_sort(x)

0 commit comments

Comments
 (0)