-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding person example UofUEpiBio/PHS7045-advanced-programming#30
- Loading branch information
Showing
2 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <Rcpp.h> | ||
|
||
class Person { | ||
public: | ||
Person(std::string name, int age) : | ||
name(name), age(age) {} | ||
std::string get_name() const { return name; } | ||
int get_age() const { return age; } | ||
void print() const {Rprintf( | ||
"%s is %d years old\n", name.c_str(), age); | ||
} | ||
|
||
private: | ||
std::string name; | ||
int age; | ||
}; | ||
|
||
// [[Rcpp::export]] | ||
Rcpp::XPtr<Person> create_person( | ||
std::string name, int age | ||
) { | ||
Rcpp::XPtr<Person> person(new Person(name, age)); | ||
|
||
// Adding an attribute | ||
person.attr("class") = "MyPerson"; | ||
|
||
return person; | ||
} | ||
|
||
// [[Rcpp::export]] | ||
std::string get_name(SEXP person) { | ||
Rcpp::XPtr<Person> p(person); | ||
return p->get_name(); | ||
} | ||
|
||
// [[Rcpp::export]] | ||
int print_person(SEXP person) { | ||
Rcpp::XPtr<Person> p(person); | ||
p->print(); | ||
return 0; | ||
} | ||
|
||
/*** R | ||
# This is an S3 method for printing MyPerson | ||
print.MyPerson <- function(x) { | ||
print_person(x) | ||
} | ||
george <- create_person("george", 123) | ||
# These two are equivalent | ||
george | ||
print(george) | ||
*/ |