I used this code to demonstrate the basics of S4 classes in R. The slides are posted here. They might shed a little more light on the code in this repo.
Use devtools!
install.packages('devtools')
devtools::install_github('alyssafrazee/cards')
Create cards with the Card
function.
aceofspades = Card(suit='spades', number=14)
aceofspades #prints "Ace of spades"
Find the card of the same color and number, but different suit, as a given card:
find_pair(aceofspades) #returns ace of clubs
Make an extremely sophisticated, artistic plot of a card:
plot(aceofspades)
I wrote this method to illustrate the concept of making a function generic (plot
already exists as an S3 method in base R) and defining its behavior for your own class.
You can see examples of Python card classes in another project I did. That project actually implements a full card game in Python, which involves several classes (card, bid, game, etc.).
Hadley Wickham's chapter on object-oriented programming in R is the place to start for OO in R. For Python, I'd start with the official docs on classes and go from there.
One important aspect of OO that I didn't cover in this little repo or in my Python project is inheritance. I've written quite a lot of R code and have yet to actually utilize inheritance in R.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.