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
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
Top 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
TopCSSGallery
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
DianaGray10
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Alkin Tezuysal
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series - Day 1
DianaGray10
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4
DianaGray10
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdf
Infopole1
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
Muhammad Tiham Siddiqui
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenarios
Erol GIRAUDY
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
IES VE
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Tejal81
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
Knoldus Inc.
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
xtailishbaloch
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First Frame
Kapil Thakar
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
Neo4j
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
Brian Pichman
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
Satishbabu Gunukula
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
shyamraj55
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)
IES VE
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Product School
Recently uploaded
(20)
Top 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series - Day 4
UiPath Studio Web workshop series - Day 4
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdf
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenarios
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Q4 2023 Quarterly Investor Presentation - FINAL - v1.pdf
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Introduction to RAG (Retrieval Augmented Generation) and its application
Introduction to RAG (Retrieval Augmented Generation) and its application
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
Flow Control | Block Size | ST Min | First Frame
Flow Control | Block Size | ST Min | First Frame
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
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