Skip to content

Commit 0b59244

Browse files
committed
Add small comment in Modules vignette following #1322
1 parent d9b5431 commit 0b59244

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2024-07-30 Dirk Eddelbuettel <[email protected]>
2+
3+
* vignettes/rmd/Rcpp-modules.Rmd (are): Add short two-sentence
4+
illustration following GitHub issue #1322
5+
16
2024-07-24 Dirk Eddelbuettel <[email protected]>
27

38
* DESCRIPTION (Version, Date): Roll micro version

vignettes/rmd/Rcpp-modules.Rmd

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: |
3-
| Exposing \proglang{C++} functions and classes
2+
title: |
3+
| Exposing \proglang{C++} functions and classes
44
| with \pkg{Rcpp} modules
55
66
# Use letters for affiliations
@@ -180,7 +180,7 @@ Consider the simple `Uniform` class below:
180180
```cpp
181181
class Uniform {
182182
public:
183-
Uniform(double min_, double max_) :
183+
Uniform(double min_, double max_) :
184184
min(min_), max(max_) {}
185185
186186
NumericVector draw(int n) {
@@ -203,15 +203,15 @@ with these two functions:
203203
using namespace Rcpp;
204204

205205
/// create external pointer to a Uniform object
206-
RcppExport SEXP Uniform__new(SEXP min_,
206+
RcppExport SEXP Uniform__new(SEXP min_,
207207
SEXP max_) {
208208
// convert inputs to appropriate C++ types
209-
double min = as<double>(min_),
209+
double min = as<double>(min_),
210210
max = as<double>(max_);
211211

212-
// create pointer to an Uniform object and
212+
// create pointer to an Uniform object and
213213
// wrap it as an external pointer
214-
Rcpp::XPtr<Uniform>
214+
Rcpp::XPtr<Uniform>
215215
ptr( new Uniform( min, max ), true );
216216

217217
// return the external pointer to the R side
@@ -220,7 +220,7 @@ RcppExport SEXP Uniform__new(SEXP min_,
220220

221221
/// invoke the draw method
222222
RcppExport SEXP Uniform__draw(SEXP xp, SEXP n_) {
223-
// grab the object as a XPtr (smart pointer)
223+
// grab the object as a XPtr (smart pointer)
224224
// to Uniform
225225
Rcpp::XPtr<Uniform> ptr(xp);
226226

@@ -252,7 +252,7 @@ DE 21 Sep 2013: there must a bug somewhere in the vignette processing
252252
```{r, eval=FALSE}
253253
f1 <- cxxfunction( , "", includes = unifModCode,
254254
plugin = "Rcpp" )
255-
getDynLib(f1) ## will display info about 'f1'
255+
getDynLib(f1) ## will display info about 'f1'
256256
```
257257

258258
The following listing shows some \textsl{manual} wrapping to access the code,
@@ -276,7 +276,7 @@ setMethod("$", "Uniform", function(x, name) {
276276
# syntactic sugar to allow new( "Uniform", ... )
277277
setMethod("initialize", "Uniform",
278278
function(.Object, ...) {
279-
.Object@pointer <-
279+
.Object@pointer <-
280280
.Call(Uniform_method("new"), ...)
281281
.Object
282282
} )
@@ -295,7 +295,7 @@ class in a way that makes both the internal
295295

296296

297297

298-
# Rcpp modules
298+
# Rcpp modules
299299

300300
The design of Rcpp modules has been influenced by \proglang{Python} modules which are generated by the
301301
`Boost.Python` library \citep{Abrahams+Grosse-Kunstleve:2003:Boost.Python}.
@@ -447,7 +447,7 @@ double norm(double x, double y) {
447447
}
448448
449449
RCPP_MODULE(mod) {
450-
function("norm", &norm,
450+
function("norm", &norm,
451451
"Provides a simple vector norm");
452452
}
453453
```
@@ -475,7 +475,7 @@ double norm(double x, double y) {
475475
RCPP_MODULE(mod_formals) {
476476
function("norm",
477477
&norm,
478-
List::create(_["x"] = 0.0,
478+
List::create(_["x"] = 0.0,
479479
_["y"] = 0.0),
480480
"Provides a simple vector norm");
481481
}
@@ -515,7 +515,7 @@ args(norm)
515515
```
516516

517517
The ellipsis (`...`) can be used to denote that additional arguments
518-
are optional; it does not take a default value.
518+
are optional; it does not take a default value.
519519

520520
```{Rcpp mod_formals3, eval=FALSE}
521521
using namespace Rcpp;
@@ -539,6 +539,11 @@ norm <- mod$norm
539539
args(norm)
540540
```
541541

542+
As of mid-2024, more recent versions of R no longer tolerate 'empty' strings
543+
as placeholders for missing arguments. It is preferable to simply not list
544+
any arguments for functions that take no arguments. Issue #1322 has an example.
545+
546+
542547
## Exposing \proglang{C++} classes using Rcpp modules
543548

544549
Rcpp modules also provide a mechanism for exposing \proglang{C++} classes, based
@@ -553,7 +558,7 @@ class may be exposed to \proglang{R} as follows:
553558
using namespace Rcpp;
554559
class Uniform {
555560
public:
556-
Uniform(double min_, double max_) :
561+
Uniform(double min_, double max_) :
557562
min(min_), max(max_) {}
558563
559564
NumericVector draw(int n) const {
@@ -680,7 +685,7 @@ The `.field_readonly` exposes a public field with read-only access from \proglan
680685
It also accepts the description of the field.
681686

682687
```cpp
683-
.field_readonly("y", &Foo::y,
688+
.field_readonly("y", &Foo::y,
684689
"documentation for y")
685690
```
686691

@@ -691,11 +696,11 @@ allowed:
691696

692697
```cpp
693698
// with getter and setter
694-
.property("z", &Foo::get_z,
699+
.property("z", &Foo::get_z,
695700
&Foo::set_z, "Documentation for z")
696701

697702
// with only getter
698-
.property("z",
703+
.property("z",
699704
&Foo::get_z, "Documentation for z")
700705
```
701706

@@ -740,7 +745,7 @@ public:
740745
}
741746
742747
IntegerVector stats() const {
743-
return
748+
return
744749
IntegerVector::create(_["read"] = nread,
745750
_["write"] = nwrite);
746751
}
@@ -822,7 +827,7 @@ to restrict the candidate methods.
822827
823828
### Special methods
824829
825-
\pkg{Rcpp} considers the methods `[[` and `[[<-` special,
830+
\pkg{Rcpp} considers the methods `[[` and `[[<-` special,
826831
and promotes them to indexing methods on the \proglang{R} side.
827832
828833
### Object finalizers
@@ -930,14 +935,14 @@ RCPP_MODULE(yada){
930935
using namespace Rcpp;
931936
932937
class_<World>("World")
933-
938+
934939
// expose the default constructor
935940
.constructor()
936941
937942
.method("greet", &World::greet)
938943
.method("set", &World::set)
939944
;
940-
945+
941946
}
942947
```
943948

@@ -1015,24 +1020,24 @@ bar$handleFoo(foo)
10151020
The following example illustrates how to use Rcpp modules to expose
10161021
the class `std::vector<double>` from the STL.
10171022

1018-
```{Rcpp mod_vec, eval=FALSE}
1019-
typedef std::vector<double> vec;
1020-
void vec_assign(vec* obj,
1023+
```{Rcpp mod_vec, eval=FALSE}
1024+
typedef std::vector<double> vec;
1025+
void vec_assign(vec* obj,
10211026
Rcpp::NumericVector data) {
10221027
obj->assign(data.begin(), data.end());
10231028
}
1024-
void vec_insert(vec* obj, int position,
1029+
void vec_insert(vec* obj, int position,
10251030
Rcpp::NumericVector data) {
10261031
vec::iterator it = obj->begin() + position;
10271032
obj->insert(it, data.begin(), data.end());
10281033
}
1029-
Rcpp::NumericVector vec_asR( vec* obj ) {
1030-
return Rcpp::wrap( *obj );
1034+
Rcpp::NumericVector vec_asR( vec* obj ) {
1035+
return Rcpp::wrap( *obj );
10311036
}
1032-
void vec_set(vec* obj, int i, double value) {
1033-
obj->at( i ) = value;
1037+
void vec_set(vec* obj, int i, double value) {
1038+
obj->at( i ) = value;
10341039
}
1035-
// Fix for C++11, where we cannot directly expose
1040+
// Fix for C++11, where we cannot directly expose
10361041
// member functions vec::resize and vec::push_back
10371042
void vec_resize (vec* obj, int n) {
10381043
obj->resize(n);
@@ -1044,7 +1049,7 @@ void vec_push_back (vec* obj, double value) {
10441049
RCPP_MODULE(mod_vec) {
10451050
using namespace Rcpp;
10461051
1047-
// we expose class std::vector<double>
1052+
// we expose class std::vector<double>
10481053
// as "vec" on the R side
10491054
class_<vec>("vec")
10501055
@@ -1066,8 +1071,8 @@ RCPP_MODULE(mod_vec) {
10661071
.const_method("front", &vec::front)
10671072
.const_method("at", &vec::at )
10681073
1069-
// exposing free functions taking a
1070-
// std::vector<double>* as their first
1074+
// exposing free functions taking a
1075+
// std::vector<double>* as their first
10711076
// argument
10721077
.method("assign", &vec_assign)
10731078
.method("insert", &vec_insert)
@@ -1233,7 +1238,7 @@ objects are exposed.
12331238

12341239
### Deprecated legacy method using loadRcppModules
12351240

1236-
Prior to release 0.9.11, where `loadModule` was introduced,
1241+
Prior to release 0.9.11, where `loadModule` was introduced,
12371242
loading all functions and classes from a module
12381243
into a package namespace was achieved using the `loadRcppModules` function
12391244
within the `.onLoad` body.
@@ -1246,7 +1251,7 @@ within the `.onLoad` body.
12461251

12471252
This will look in the package's `DESCRIPTION` file for the `RcppModules`
12481253
field, load each declared module and populate their contents into the
1249-
package's namespace. For example, a package defining modules
1254+
package's namespace. For example, a package defining modules
12501255
`yada`, `stdVector`, `NumEx` would have this declaration:
12511256

12521257
```
@@ -1346,5 +1351,3 @@ This note introduced \textsl{Rcpp modules} and illustrated how to expose
13461351
\proglang{C++} function and classes more easily to \proglang{R}.
13471352
We hope that \proglang{R} and \proglang{C++} programmers
13481353
find \textsl{Rcpp modules} useful.
1349-
1350-

0 commit comments

Comments
 (0)