Are there obvious reasons to choose between methods and functions in R?
I've written a function to return a class I built that contains some calculations for the data passed to the function.
Once the new object is returned, I intend to print out some of the data in a little "report" and then map the lines contained in the sf slot colored by an attribute the original function calculated.
carbon_class <- setClass("carbon_class", slots = c(total_carbon = "numeric", carbon_by_type = "data.frame", trips = "sf"), contains = c("data.frame", "sf"))
I was going to define two methods for the class to create the report and map, mainly to practice object oriented programming in R, but as I'm reading about it, I'm having a hard time coming up with a reason to use a method instead of just another function.
Are there obvious use cases for each? I'm reading through Hadley's Advanced R and it talks about how to use the S3 and S4 classes/methods, but not why.
Thanks
edit: is it for using side effects because technically functions should only return a value without any side effects while its more acceptable for methods to do other things in addition to what they return?
r oop methods
add a comment |
I've written a function to return a class I built that contains some calculations for the data passed to the function.
Once the new object is returned, I intend to print out some of the data in a little "report" and then map the lines contained in the sf slot colored by an attribute the original function calculated.
carbon_class <- setClass("carbon_class", slots = c(total_carbon = "numeric", carbon_by_type = "data.frame", trips = "sf"), contains = c("data.frame", "sf"))
I was going to define two methods for the class to create the report and map, mainly to practice object oriented programming in R, but as I'm reading about it, I'm having a hard time coming up with a reason to use a method instead of just another function.
Are there obvious use cases for each? I'm reading through Hadley's Advanced R and it talks about how to use the S3 and S4 classes/methods, but not why.
Thanks
edit: is it for using side effects because technically functions should only return a value without any side effects while its more acceptable for methods to do other things in addition to what they return?
r oop methods
1
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
2
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics likeplot
andprint
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by usingplot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like[
and[<-
may also be good practice
– dww
Jan 1 at 17:47
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54
add a comment |
I've written a function to return a class I built that contains some calculations for the data passed to the function.
Once the new object is returned, I intend to print out some of the data in a little "report" and then map the lines contained in the sf slot colored by an attribute the original function calculated.
carbon_class <- setClass("carbon_class", slots = c(total_carbon = "numeric", carbon_by_type = "data.frame", trips = "sf"), contains = c("data.frame", "sf"))
I was going to define two methods for the class to create the report and map, mainly to practice object oriented programming in R, but as I'm reading about it, I'm having a hard time coming up with a reason to use a method instead of just another function.
Are there obvious use cases for each? I'm reading through Hadley's Advanced R and it talks about how to use the S3 and S4 classes/methods, but not why.
Thanks
edit: is it for using side effects because technically functions should only return a value without any side effects while its more acceptable for methods to do other things in addition to what they return?
r oop methods
I've written a function to return a class I built that contains some calculations for the data passed to the function.
Once the new object is returned, I intend to print out some of the data in a little "report" and then map the lines contained in the sf slot colored by an attribute the original function calculated.
carbon_class <- setClass("carbon_class", slots = c(total_carbon = "numeric", carbon_by_type = "data.frame", trips = "sf"), contains = c("data.frame", "sf"))
I was going to define two methods for the class to create the report and map, mainly to practice object oriented programming in R, but as I'm reading about it, I'm having a hard time coming up with a reason to use a method instead of just another function.
Are there obvious use cases for each? I'm reading through Hadley's Advanced R and it talks about how to use the S3 and S4 classes/methods, but not why.
Thanks
edit: is it for using side effects because technically functions should only return a value without any side effects while its more acceptable for methods to do other things in addition to what they return?
r oop methods
r oop methods
asked Jan 1 at 13:06


Hugh_KelleyHugh_Kelley
102110
102110
1
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
2
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics likeplot
andprint
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by usingplot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like[
and[<-
may also be good practice
– dww
Jan 1 at 17:47
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54
add a comment |
1
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
2
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics likeplot
andprint
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by usingplot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like[
and[<-
may also be good practice
– dww
Jan 1 at 17:47
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54
1
1
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
2
2
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics like
plot
and print
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by using plot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like [
and [<-
may also be good practice– dww
Jan 1 at 17:47
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics like
plot
and print
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by using plot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like [
and [<-
may also be good practice– dww
Jan 1 at 17:47
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995685%2fare-there-obvious-reasons-to-choose-between-methods-and-functions-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995685%2fare-there-obvious-reasons-to-choose-between-methods-and-functions-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Maybe this will be interesting to you: youtube.com/watch?v=93N0HdoZW9g
– Ricardo Fernandes Campos
Jan 1 at 14:45
2
I think the most obvious case when a method is clearly preferable is to make life easier and more intuitive to end users with generics like
plot
andprint
. people are used to being able to type an object's name at the console and have some interesting and useful info reported back about the object at the console. Similarly, people are used to being able to obtain a useful graphical interpretation of an object by usingplot
. Providing users with these functionalities seems good practice. If appropriate, providing versions of other generics like[
and[<-
may also be good practice– dww
Jan 1 at 17:47
haven't had a chance to watch that video but looks interesting so thank you. The generic function example makes a lot of sense. Hadn't given any thought to how plot() could handle nearly anything that was passed to it.
– Hugh_Kelley
Jan 1 at 22:54