Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Modules vignette review#982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Complete World S4 dispatch example.
* World was not already included in the examples above.
  • Loading branch information
@riccardoporreca
riccardoporreca committedAug 5, 2019
commit54cb17a1f9cce4e3a6815e67572c1259c3fa15d2
37 changes: 33 additions & 4 deletionsvignettes/Rcpp-modules.Rmd
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -912,18 +912,47 @@ dv2$name() # returns "Derived2"
When a \proglang{C++} class is exposed by the `class_` template,
a new S4 class is registered as well. The name of the S4 class is
obfuscated in order to avoid name clashes (i.e. two modules exposing the
same class).
same class). This allows implementation of \proglang{R}-level
(S4) dispatch.

This allows implementation of \proglang{R}-level
(S4) dispatch. For example, one might implement the `show`
method for \proglang{C++} `World` objects from the module `yada` created above:
For example, consider the \proglang{C++} class `World` exposed in module `yada`:

```cpp
class World {
public:
World() : msg("hello") {}
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }

private:
std::string msg;
};

RCPP_MODULE(yada){
using namespace Rcpp ;

class_<World>("World")

// expose the default constructor
.constructor()

.method("greet", &World::greet)
.method("set", &World::set)
;

}
```

The `show` method for `World` objects can then implemented as:

```{r, eval=FALSE}
yada <- Module("yada", inline::getDynLib(fx))
setMethod("show", yada$World , function(object) {
msg <- paste("World object with message : ",
object$greet())
writeLines(msg)
} )
yada$World$new() # implictly calls show
```

TODO: mention R inheritance (John ?)
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp