How to write generic function with two inputs?How to sort a dataframe by multiple column(s)How to join (merge) data frames (inner, outer, left, right)Grouping functions (tapply, by, aggregate) and the *apply familyHow to make a great R reproducible exampleArguments and classes for writing (generic) functions in RWriting generic function for tables that works when the input happens to be vectorHow to retrieve formals of a primitive function?Error within function using solve() in RSubsetting data as generic function in RWriting if / ifelse function in R
What are the advantages and disadvantages of running one shots compared to campaigns?
What does "enim et" mean?
Could a US political party gain complete control over the government by removing checks & balances?
Domain expired, GoDaddy holds it and is asking more money
Copycat chess is back
How to handle columns with categorical data and many unique values
Email Account under attack (really) - anything I can do?
Where to refill my bottle in India?
How to make payment on the internet without leaving a money trail?
Is it possible to make sharp wind that can cut stuff from afar?
Is this homebrew feat, Beast of Burden, balanced?
Wild Shape Centaur Into a Giant Elk: do their Charges stack?
aging parents with no investments
How do you conduct xenoanthropology after first contact?
What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?
Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?
how can we implement methods in multiples classes if we add methods in interface
Does the average primeness of natural numbers tend to zero?
Weird behaviour when using querySelector
I am not able to install anything in ubuntu
Can I interfere when another PC is about to be attacked?
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
What to wear for invited talk in Canada
When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?
How to write generic function with two inputs?
How to sort a dataframe by multiple column(s)How to join (merge) data frames (inner, outer, left, right)Grouping functions (tapply, by, aggregate) and the *apply familyHow to make a great R reproducible exampleArguments and classes for writing (generic) functions in RWriting generic function for tables that works when the input happens to be vectorHow to retrieve formals of a primitive function?Error within function using solve() in RSubsetting data as generic function in RWriting if / ifelse function in R
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am a newbee in programming, and I run into an issue with R about generic function: how to write it when there are multiple inputs?
For an easy example, for dataset and function
z <- c(2,3,4,5,8)
calc.simp <- function(a,x)a*x+8
# Test the function:
calc.simp(x=z,a=3)
[1] 14 17 20 23 32
Now I change the class of z:
class(z) <- 'simp'
How should I write the generic function 'calc' as there are two inputs?
My attempts and errors are below:
calc <- function(x) UseMethod('calc',x)
calc(x=z)
Error in calc.simp(x = z) : argument "a" is missing, with no default
And
calc <- function(x,y) UseMethod('calc',x,y)
Error in UseMethod("calc", x, y) : unused argument (y)
My confusion might be a fundamental one as I am just a beginner. Please help! Thank you very much!
r generic-programming
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am a newbee in programming, and I run into an issue with R about generic function: how to write it when there are multiple inputs?
For an easy example, for dataset and function
z <- c(2,3,4,5,8)
calc.simp <- function(a,x)a*x+8
# Test the function:
calc.simp(x=z,a=3)
[1] 14 17 20 23 32
Now I change the class of z:
class(z) <- 'simp'
How should I write the generic function 'calc' as there are two inputs?
My attempts and errors are below:
calc <- function(x) UseMethod('calc',x)
calc(x=z)
Error in calc.simp(x = z) : argument "a" is missing, with no default
And
calc <- function(x,y) UseMethod('calc',x,y)
Error in UseMethod("calc", x, y) : unused argument (y)
My confusion might be a fundamental one as I am just a beginner. Please help! Thank you very much!
r generic-programming
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
What do you expect to be returned fromcalc(x=z)? You aren't giving your function a value foraand your function depends on it. Also you can let your generic function know there may be other argumets withcalc <- function(x, ...) UseMethod('calc',x)
– MrFlick
Apr 3 at 19:45
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24
add a comment |
I am a newbee in programming, and I run into an issue with R about generic function: how to write it when there are multiple inputs?
For an easy example, for dataset and function
z <- c(2,3,4,5,8)
calc.simp <- function(a,x)a*x+8
# Test the function:
calc.simp(x=z,a=3)
[1] 14 17 20 23 32
Now I change the class of z:
class(z) <- 'simp'
How should I write the generic function 'calc' as there are two inputs?
My attempts and errors are below:
calc <- function(x) UseMethod('calc',x)
calc(x=z)
Error in calc.simp(x = z) : argument "a" is missing, with no default
And
calc <- function(x,y) UseMethod('calc',x,y)
Error in UseMethod("calc", x, y) : unused argument (y)
My confusion might be a fundamental one as I am just a beginner. Please help! Thank you very much!
r generic-programming
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am a newbee in programming, and I run into an issue with R about generic function: how to write it when there are multiple inputs?
For an easy example, for dataset and function
z <- c(2,3,4,5,8)
calc.simp <- function(a,x)a*x+8
# Test the function:
calc.simp(x=z,a=3)
[1] 14 17 20 23 32
Now I change the class of z:
class(z) <- 'simp'
How should I write the generic function 'calc' as there are two inputs?
My attempts and errors are below:
calc <- function(x) UseMethod('calc',x)
calc(x=z)
Error in calc.simp(x = z) : argument "a" is missing, with no default
And
calc <- function(x,y) UseMethod('calc',x,y)
Error in UseMethod("calc", x, y) : unused argument (y)
My confusion might be a fundamental one as I am just a beginner. Please help! Thank you very much!
r generic-programming
r generic-programming
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Apr 3 at 19:36
Branda NewbeeBranda Newbee
584
584
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Branda Newbee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
What do you expect to be returned fromcalc(x=z)? You aren't giving your function a value foraand your function depends on it. Also you can let your generic function know there may be other argumets withcalc <- function(x, ...) UseMethod('calc',x)
– MrFlick
Apr 3 at 19:45
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24
add a comment |
1
What do you expect to be returned fromcalc(x=z)? You aren't giving your function a value foraand your function depends on it. Also you can let your generic function know there may be other argumets withcalc <- function(x, ...) UseMethod('calc',x)
– MrFlick
Apr 3 at 19:45
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24
1
1
What do you expect to be returned from
calc(x=z)? You aren't giving your function a value for a and your function depends on it. Also you can let your generic function know there may be other argumets with calc <- function(x, ...) UseMethod('calc',x)– MrFlick
Apr 3 at 19:45
What do you expect to be returned from
calc(x=z)? You aren't giving your function a value for a and your function depends on it. Also you can let your generic function know there may be other argumets with calc <- function(x, ...) UseMethod('calc',x)– MrFlick
Apr 3 at 19:45
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24
add a comment |
1 Answer
1
active
oldest
votes
I'd suggest you model your generic function off of the template used by innumerable base R functions as, e.g., mean:
> mean
function (x, ...)
UseMethod("mean")
In your case, that would translate to the following generic which (if I understand your question correctly) works just fine:
calc <- function(x, ...) UseMethod('calc')
calc.simp <- function(a, x)
x <- unclass(x)
a * x + 8
## Try it out
z <- c(2,3,4,5,8)
class(z) <- "simp"
calc.simp(x = z, 10)
## [1] 28 38 48 58 88
calc(x = z, 10)
## [1] 28 38 48 58 88
add a comment |
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
);
);
Branda Newbee is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55503025%2fhow-to-write-generic-function-with-two-inputs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'd suggest you model your generic function off of the template used by innumerable base R functions as, e.g., mean:
> mean
function (x, ...)
UseMethod("mean")
In your case, that would translate to the following generic which (if I understand your question correctly) works just fine:
calc <- function(x, ...) UseMethod('calc')
calc.simp <- function(a, x)
x <- unclass(x)
a * x + 8
## Try it out
z <- c(2,3,4,5,8)
class(z) <- "simp"
calc.simp(x = z, 10)
## [1] 28 38 48 58 88
calc(x = z, 10)
## [1] 28 38 48 58 88
add a comment |
I'd suggest you model your generic function off of the template used by innumerable base R functions as, e.g., mean:
> mean
function (x, ...)
UseMethod("mean")
In your case, that would translate to the following generic which (if I understand your question correctly) works just fine:
calc <- function(x, ...) UseMethod('calc')
calc.simp <- function(a, x)
x <- unclass(x)
a * x + 8
## Try it out
z <- c(2,3,4,5,8)
class(z) <- "simp"
calc.simp(x = z, 10)
## [1] 28 38 48 58 88
calc(x = z, 10)
## [1] 28 38 48 58 88
add a comment |
I'd suggest you model your generic function off of the template used by innumerable base R functions as, e.g., mean:
> mean
function (x, ...)
UseMethod("mean")
In your case, that would translate to the following generic which (if I understand your question correctly) works just fine:
calc <- function(x, ...) UseMethod('calc')
calc.simp <- function(a, x)
x <- unclass(x)
a * x + 8
## Try it out
z <- c(2,3,4,5,8)
class(z) <- "simp"
calc.simp(x = z, 10)
## [1] 28 38 48 58 88
calc(x = z, 10)
## [1] 28 38 48 58 88
I'd suggest you model your generic function off of the template used by innumerable base R functions as, e.g., mean:
> mean
function (x, ...)
UseMethod("mean")
In your case, that would translate to the following generic which (if I understand your question correctly) works just fine:
calc <- function(x, ...) UseMethod('calc')
calc.simp <- function(a, x)
x <- unclass(x)
a * x + 8
## Try it out
z <- c(2,3,4,5,8)
class(z) <- "simp"
calc.simp(x = z, 10)
## [1] 28 38 48 58 88
calc(x = z, 10)
## [1] 28 38 48 58 88
edited Apr 3 at 20:04
answered Apr 3 at 19:59
Josh O'BrienJosh O'Brien
130k18281391
130k18281391
add a comment |
add a comment |
Branda Newbee is a new contributor. Be nice, and check out our Code of Conduct.
Branda Newbee is a new contributor. Be nice, and check out our Code of Conduct.
Branda Newbee is a new contributor. Be nice, and check out our Code of Conduct.
Branda Newbee is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55503025%2fhow-to-write-generic-function-with-two-inputs%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
What do you expect to be returned from
calc(x=z)? You aren't giving your function a value foraand your function depends on it. Also you can let your generic function know there may be other argumets withcalc <- function(x, ...) UseMethod('calc',x)– MrFlick
Apr 3 at 19:45
What do you want your function to do? Your first function (calc.simp) still works even after changing the class of z.
– Luis
Apr 3 at 19:49
@MrFlick I simply want to test whether my generic function can work! It helps me understand the dispatch mechanism better. The 'function(x,...)' works perfectly for my question. Thank you so much! :)
– Branda Newbee
Apr 3 at 19:58
question is about dispatching? didn't see this keyword anywhere on this page, hence adding it here.
– chinsoon12
Apr 4 at 0:24