Submit Search
Upload
Object Oriented Design(s) in R
•
7 likes
•
3,651 views
Romain Francois
Follow
Various ways to represent objects in R
Read less
Read more
Technology
Report
Share
Report
Share
1 of 25
Download now
Download to read offline
Recommended
Java Se next Generetion
Java Se next Generetion
Otávio Santana
The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
A bit about Scala
A bit about Scala
Vladimir Parfinenko
The Ring programming language version 1.6 book - Part 39 of 189
The Ring programming language version 1.6 book - Part 39 of 189
Mahmoud Samir Fayed
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
DevClub_lv
The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212
Mahmoud Samir Fayed
The Ring programming language version 1.8 book - Part 35 of 202
The Ring programming language version 1.8 book - Part 35 of 202
Mahmoud Samir Fayed
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
jorgeortiz85
Recommended
Java Se next Generetion
Java Se next Generetion
Otávio Santana
The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84
Mahmoud Samir Fayed
A bit about Scala
A bit about Scala
Vladimir Parfinenko
The Ring programming language version 1.6 book - Part 39 of 189
The Ring programming language version 1.6 book - Part 39 of 189
Mahmoud Samir Fayed
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
“SOLID principles in PHP – how to apply them in PHP and why should we care“ b...
DevClub_lv
The Ring programming language version 1.10 book - Part 47 of 212
The Ring programming language version 1.10 book - Part 47 of 212
Mahmoud Samir Fayed
The Ring programming language version 1.8 book - Part 35 of 202
The Ring programming language version 1.8 book - Part 35 of 202
Mahmoud Samir Fayed
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
jorgeortiz85
The Ring programming language version 1.9 book - Part 38 of 210
The Ring programming language version 1.9 book - Part 38 of 210
Mahmoud Samir Fayed
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180
Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 30 of 185
The Ring programming language version 1.5.4 book - Part 30 of 185
Mahmoud Samir Fayed
CS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDB
jorgeortiz85
The Ring programming language version 1.3 book - Part 28 of 88
The Ring programming language version 1.3 book - Part 28 of 88
Mahmoud Samir Fayed
Polynomial
Polynomial
Emmanuel Fuchs
The Ring programming language version 1.2 book - Part 23 of 84
The Ring programming language version 1.2 book - Part 23 of 84
Mahmoud Samir Fayed
Xm lparsers
Xm lparsers
Suman Lata
RESTful API using scalaz (3)
RESTful API using scalaz (3)
Yeshwanth Kumar
Seductions of Scala
Seductions of Scala
Dean Wampler
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
2013 - Benjamin Eberlei - Doctrine 2
2013 - Benjamin Eberlei - Doctrine 2
PHP Conference Argentina
The Ring programming language version 1.3 book - Part 21 of 88
The Ring programming language version 1.3 book - Part 21 of 88
Mahmoud Samir Fayed
The Ring programming language version 1.5.3 book - Part 37 of 184
The Ring programming language version 1.5.3 book - Part 37 of 184
Mahmoud Samir Fayed
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
stasimus
JavaScript Web Development
JavaScript Web Development
vito jeng
Java and j2ee_lab-manual
Java and j2ee_lab-manual
hanumanthu mothukuru
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa
Legacy lambda code
Legacy lambda code
Peter Lawrey
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
Peter Lawrey
Scala by Luc Duponcheel
Scala by Luc Duponcheel
Stephan Janssen
Refactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
More Related Content
What's hot
The Ring programming language version 1.9 book - Part 38 of 210
The Ring programming language version 1.9 book - Part 38 of 210
Mahmoud Samir Fayed
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180
Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 30 of 185
The Ring programming language version 1.5.4 book - Part 30 of 185
Mahmoud Samir Fayed
CS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDB
jorgeortiz85
The Ring programming language version 1.3 book - Part 28 of 88
The Ring programming language version 1.3 book - Part 28 of 88
Mahmoud Samir Fayed
Polynomial
Polynomial
Emmanuel Fuchs
The Ring programming language version 1.2 book - Part 23 of 84
The Ring programming language version 1.2 book - Part 23 of 84
Mahmoud Samir Fayed
Xm lparsers
Xm lparsers
Suman Lata
RESTful API using scalaz (3)
RESTful API using scalaz (3)
Yeshwanth Kumar
Seductions of Scala
Seductions of Scala
Dean Wampler
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
Mahmoud Samir Fayed
2013 - Benjamin Eberlei - Doctrine 2
2013 - Benjamin Eberlei - Doctrine 2
PHP Conference Argentina
The Ring programming language version 1.3 book - Part 21 of 88
The Ring programming language version 1.3 book - Part 21 of 88
Mahmoud Samir Fayed
The Ring programming language version 1.5.3 book - Part 37 of 184
The Ring programming language version 1.5.3 book - Part 37 of 184
Mahmoud Samir Fayed
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
stasimus
JavaScript Web Development
JavaScript Web Development
vito jeng
Java and j2ee_lab-manual
Java and j2ee_lab-manual
hanumanthu mothukuru
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa
Legacy lambda code
Legacy lambda code
Peter Lawrey
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
Peter Lawrey
What's hot
(20)
The Ring programming language version 1.9 book - Part 38 of 210
The Ring programming language version 1.9 book - Part 38 of 210
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.1 book - Part 43 of 180
The Ring programming language version 1.5.4 book - Part 30 of 185
The Ring programming language version 1.5.4 book - Part 30 of 185
CS442 - Rogue: A Scala DSL for MongoDB
CS442 - Rogue: A Scala DSL for MongoDB
The Ring programming language version 1.3 book - Part 28 of 88
The Ring programming language version 1.3 book - Part 28 of 88
Polynomial
Polynomial
The Ring programming language version 1.2 book - Part 23 of 84
The Ring programming language version 1.2 book - Part 23 of 84
Xm lparsers
Xm lparsers
RESTful API using scalaz (3)
RESTful API using scalaz (3)
Seductions of Scala
Seductions of Scala
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
2013 - Benjamin Eberlei - Doctrine 2
2013 - Benjamin Eberlei - Doctrine 2
The Ring programming language version 1.3 book - Part 21 of 88
The Ring programming language version 1.3 book - Part 21 of 88
The Ring programming language version 1.5.3 book - Part 37 of 184
The Ring programming language version 1.5.3 book - Part 37 of 184
Groovy vs Boilerplate and Ceremony Code
Groovy vs Boilerplate and Ceremony Code
JavaScript Web Development
JavaScript Web Development
Java and j2ee_lab-manual
Java and j2ee_lab-manual
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Legacy lambda code
Legacy lambda code
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the ugly
Similar to Object Oriented Design(s) in R
Scala by Luc Duponcheel
Scala by Luc Duponcheel
Stephan Janssen
Refactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
Functional Principles for OO Developers
Functional Principles for OO Developers
jessitron
Clojure functions examples
Clojure functions examples
Jackson dos Santos Olveira
Smarter Testing With Spock
Smarter Testing With Spock
IT Weekend
C++ process new
C++ process new
敬倫 林
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
Chris Seaton
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2
RORLAB
Clean Architecture Applications in Python
Clean Architecture Applications in Python
Subhash Bhushan
Improving application design with a rich domain model (springone 2007)
Improving application design with a rich domain model (springone 2007)
Chris Richardson
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
Felinx Lee
More to RoC weibo
More to RoC weibo
shaokun
Android DevConference - Android Clean Architecture
Android DevConference - Android Clean Architecture
iMasters
exportDisabledUsersRemoveMailbox
exportDisabledUsersRemoveMailbox
Daniel Gilhousen
R tutorial (R program 101)
R tutorial (R program 101)
Gregory Choi, MBA, CISSP
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
Mahmoud Samir Fayed
Pellucid stm
Pellucid stm
Dustin Whitney
Presentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
Apache Spark for Library Developers with William Benton and Erik Erlandson
Apache Spark for Library Developers with William Benton and Erik Erlandson
Databricks
Compose Async with RxJS
Compose Async with RxJS
Kyung Yeol Kim
Similar to Object Oriented Design(s) in R
(20)
Scala by Luc Duponcheel
Scala by Luc Duponcheel
Refactoring to Macros with Clojure
Refactoring to Macros with Clojure
Functional Principles for OO Developers
Functional Principles for OO Developers
Clojure functions examples
Clojure functions examples
Smarter Testing With Spock
Smarter Testing With Spock
C++ process new
C++ process new
The Future Shape of Ruby Objects
The Future Shape of Ruby Objects
ActiveRecord Query Interface (2), Season 2
ActiveRecord Query Interface (2), Season 2
Clean Architecture Applications in Python
Clean Architecture Applications in Python
Improving application design with a rich domain model (springone 2007)
Improving application design with a rich domain model (springone 2007)
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
More to RoC weibo
More to RoC weibo
Android DevConference - Android Clean Architecture
Android DevConference - Android Clean Architecture
exportDisabledUsersRemoveMailbox
exportDisabledUsersRemoveMailbox
R tutorial (R program 101)
R tutorial (R program 101)
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
Pellucid stm
Pellucid stm
Presentation on C++ Programming Language
Presentation on C++ Programming Language
Apache Spark for Library Developers with William Benton and Erik Erlandson
Apache Spark for Library Developers with William Benton and Erik Erlandson
Compose Async with RxJS
Compose Async with RxJS
More from Romain Francois
R/C++
R/C++
Romain Francois
dplyr and torrents from cpasbien
dplyr and torrents from cpasbien
Romain Francois
dplyr use case
dplyr use case
Romain Francois
dplyr
dplyr
Romain Francois
user2015 keynote talk
user2015 keynote talk
Romain Francois
SevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittr
Romain Francois
dplyr
dplyr
Romain Francois
Data manipulation with dplyr
Data manipulation with dplyr
Romain Francois
R/C++ talk at earl 2014
R/C++ talk at earl 2014
Romain Francois
Rcpp11 genentech
Rcpp11 genentech
Romain Francois
Rcpp11 useR2014
Rcpp11 useR2014
Romain Francois
Rcpp11
Rcpp11
Romain Francois
R and C++
R and C++
Romain Francois
R and cpp
R and cpp
Romain Francois
Rcpp attributes
Rcpp attributes
Romain Francois
Rcpp is-ready
Rcpp is-ready
Romain Francois
Rcpp
Rcpp
Romain Francois
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
Romain Francois
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
Romain Francois
RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
Romain Francois
More from Romain Francois
(20)
R/C++
R/C++
dplyr and torrents from cpasbien
dplyr and torrents from cpasbien
dplyr use case
dplyr use case
dplyr
dplyr
user2015 keynote talk
user2015 keynote talk
SevillaR meetup: dplyr and magrittr
SevillaR meetup: dplyr and magrittr
dplyr
dplyr
Data manipulation with dplyr
Data manipulation with dplyr
R/C++ talk at earl 2014
R/C++ talk at earl 2014
Rcpp11 genentech
Rcpp11 genentech
Rcpp11 useR2014
Rcpp11 useR2014
Rcpp11
Rcpp11
R and C++
R and C++
R and cpp
R and cpp
Rcpp attributes
Rcpp attributes
Rcpp is-ready
Rcpp is-ready
Rcpp
Rcpp
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
RProtoBuf: protocol buffers for R
RProtoBuf: protocol buffers for R
Recently uploaded
🐬 The future of MySQL is Postgres 🐘
🐬 The future of MySQL is Postgres 🐘
RTylerCroy
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
naman860154
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Safe Software
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Principled Technologies
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
The Digital Insurer
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Miguel Araújo
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
Enterprise Knowledge
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Malak Abu Hammad
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
HampshireHUG
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Rafal Los
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Martijn de Jong
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
The Digital Insurer
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
Radu Cotescu
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
Pooja Nehwal
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
The Digital Insurer
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Delhi Call girls
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Katpro Technologies
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
Results
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
apidays
Recently uploaded
(20)
🐬 The future of MySQL is Postgres 🐘
🐬 The future of MySQL is Postgres 🐘
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Object Oriented Design(s) in R
1.
Object Oriented Design(s)
in R Romain François romain@r-enthusiasts.com Chicago Local R User Group, Oct 27th , Chicago.
2.
Outline
Lexical Scoping S3 classes S4 classes Reference (R5) classes C++ classes Protocol Buffers
3.
Fil rouge: bank
account example Data: - The balance - Authorized overdraft Operations: - Open an account - Get the balance - Deposit - Withdraw .
4.
Lexical Scoping
5.
> open.account <-
function(total, overdraft = 0.0){ + deposit <- function(amount) { + if( amount < 0 ) + stop( "deposits must be positive" ) + total <<- total + amount + } + withdraw <- function(amount) { + if( amount < 0 ) + stop( "withdrawals must be positive" ) + if( total - amount < overdraft ) + stop( "you cannot withdraw that much" ) + total <<- total - amount + } + balance <- function() { + total + } + list( deposit = deposit, withdraw = withdraw, + balance = balance ) + } > romain <- open.account(500) > romain$balance() [1] 500 > romain$deposit(100) > romain$withdraw(200) > romain$balance() [1] 400
6.
S3 classes
7.
S3 classes
Any R object with a class attribute Very easy Very dangerous Behaviour is added through S3 generic functions > Account <- function( total, overdraft = 0.0 ){ + out <- list( balance = total, overdraft = overdraft ) + class( out ) <- "Account" + out + } > balance <- function(x){ + UseMethod( "balance" ) + } > balance.Account <- function(x) x$balance
8.
S3 classes
> deposit <- function(x, amount){ + UseMethod( "deposit" ) + } > deposit.Account <- function(x, amount) { + if( amount < 0 ) + stop( "deposits must be positive" ) + x$balance <- x$balance + amount + x + } > withdraw <- function(x, amount){ + UseMethod( "withdraw" ) + } > withdraw.Account <- function(x, amount) { + if( amount < 0 ) + stop( "withdrawals must be positive" ) + if( x$balance - amount < x$overdraft ) + stop( "you cannot withdraw that much" ) + x$balance <- x$balance - amount + x + }
9.
S3 classes
Example use: > romain <- Account( 500 ) > balance( romain ) [1] 500 > romain <- deposit( romain, 100 ) > romain <- withdraw( romain, 200 ) > balance( romain ) [1] 400
10.
S4 classes
11.
S4 classes
Formal class definition Validity checking Formal generic functions and methods Very verbose, both in code and documentation
12.
S4 classes
> setClass( "Account", + representation( + balance = "numeric", + overdraft = "numeric" + ), + prototype = prototype( + balance = 0.0, + overdraft = 0.0 + ), + validity = function(object){ + object@balance > object@overdraft + } + ) [1] "Account" > setGeneric( "balance", + function(x) standardGeneric( "balance" ) + ) [1] "balance" > setMethod( "balance", "Account", + function(x) x@balance + ) [1] "balance"
13.
S4 classes
> setGeneric( "deposit", + function(x, amount) standardGeneric( "deposit" ) + ) [1] "deposit" > setMethod( "deposit", + signature( x = "Account", amount = "numeric" ), + function(x, amount){ + new( "Account" , + balance = x@balance + amount, + overdraft = x@overdraft + ) + } + ) [1] "deposit"
14.
S4 classes
> romain <- new( "Account", balance = 500 ) > balance( romain ) [1] 500 > romain <- deposit( romain, 100 ) > romain <- withdraw( romain, 200 ) > balance( romain ) [1] 400
15.
Reference (R5) classes
16.
Reference (R5) classes
Real S4 classes: formalism, dispatch, ... Passed by Reference Easy to use
17.
Outline Fil rouge
lexical scoping S3 classes S4 classes Reference (R5) classes C++ classes Protocol Buffers > Account <- setRefClass( "Account_R5", + fields = list( + balance = "numeric", + overdraft = "numeric" + ), + methods = list( + withdraw = function( amount ){ + if( amount < 0 ) + stop( "withdrawal must be positive" ) + if( balance - amount < overdraft ) + stop( "overdrawn" ) + balance <<- balance - amount + }, + deposit = function(amount){ + if( amount < 0 ) + stop( "deposits must be positive" ) + balance <<- balance + amount + } + ) + ) > x <- Account$new( balance = 10.0, overdraft = 0.0 ) > x$withdraw( 5 ) > x$deposit( 10 ) > x$balance [1] 15 Romain François Objects @ Chiacgo R User Group/Oct 2010
18.
Outline Fil rouge
lexical scoping S3 classes S4 classes Reference (R5) classes C++ classes Protocol Buffers Real pass by reference : > borrow <- function( x, y, amount = 0.0 ){ + x$withdraw( amount ) + y$deposit( amount ) + invisible(NULL) + } > romain <- Account$new( balance = 5000, overdraft = 0.0 ) > dirk <- Account$new( balance = 3, overdraft = 0.0 ) > borrow( romain, dirk, 2000 ) > romain$balance [1] 3000 > dirk$balance [1] 2003 Romain François Objects @ Chiacgo R User Group/Oct 2010
19.
Outline Fil rouge
lexical scoping S3 classes S4 classes Reference (R5) classes C++ classes Protocol Buffers Adding a method dynamically to a class : > Account$methods( + borrow = function(other, amount){ + deposit( amount ) + other$withdraw( amount ) + invisible(NULL) + } + ) > romain <- Account$new( balance = 5000, overdraft = 0.0 ) > dirk <- Account$new( balance = 3, overdraft = 0.0 ) > dirk$borrow( romain, 2000 ) > romain$balance [1] 3000 > dirk$balance [1] 2003 Romain François Objects @ Chiacgo R User Group/Oct 2010
20.
C++ classes
21.
C++ classes
class Account { public: Account() : balance(0.0), overdraft(0.0){} void withdraw( double amount ){ if( balance - amount < overdraft ) throw std::range_error( "no way") ; balance -= amount ; } void deposit( double amount ){ balance += amount ; } double balance ; private: double overdraft ; } ;
22.
C++ classes
Exposing to R through Rcpp modules: RCPP_MODULE(yada){ class_<Account>( "Account") // expose the field .field_readonly( "balance", &Account::balance ) // expose the methods .method( "withdraw", &Account::withdraw ) .method( "deposit", &Account::deposit ) ; } Use it in R: > Account <- yada$Account > romain <- Account$new() > romain$deposit( 10 ) > romain$withdraw( 2 ) > romain$balance [1] 8
23.
Protocol Buffers
24.
Protocol Buffers
Define the message type, in Account.proto : package foo ; message Account { required double balance = 1 ; required double overdraft = 2 ; } Load it into R with RProtoBuf: > require( RProtoBuf ) > loadProtoFile( "Account.proto" ) Use it: > romain <- new( foo.Account, + balance = 500, overdraft = 10 ) > romain$balance
25.
Questions ?
Romain François http://romainfrancois.blog.free.fr romain@r-enthusiasts.com Chicago Local R User Group, Oct 27th , Chicago.
Download now