-
Notifications
You must be signed in to change notification settings - Fork 18
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Advantages of object oriented coding in R? #31
Comments
@DanOvando Interesting question. I don't know for certain, but I'll hazard a guess... The main advantages to defining your own classes -- particularly S4 objects -- are that they can/will impose arbitrarily strict functionality and checks on your objects. This can be useful, for example, if you require a special series of tests or statistical checks, or if you want your output to look a specific way. (Think tibble versus a normal dataframe... Or a coeftest object from the Why would you need to do this for your hypothetical "myfish" class? It's hard to say without more information, but one possibility is that the authors wanted to make sure that these objects contained a full set of predefined slots, which in turn might be important for some kind of statistical or spatial analysis... |
I haven't used object-oriented coding in R, but from what I recall from C++ and another object-oriented language I used long ago, it could be thought of as a specific data structure (like a list with pre-defined named elements), for which you can define functions to interact with that data in a specific way.
Some possible advantages, which seem more for other users than necessarily for yourself, unless you're going to use this data all over the place:
|
Hi gang,
A few folks code that I've been using makes use of object oriented coding in R via defining S4 classes for objects. e.g. you have an object defined as class
fish
calledmyfish
that has slotsmyfish@common_name
,myfish@max_length
, etc.It all looks very snazzy, but I'm a little unclear as to the relative benefits of this vs. say a list, where now I define a list called
myfish
with elementsmyfish$common_name
,myfish$max_length
, etc.The list format is more in line with the standard way we program in R, and is more amenable to the tidyverse, so wondering with the main advantages of the object-oriented approach are?
Any thoughts?
The text was updated successfully, but these errors were encountered: