Skip to content

Default Generic function for: args(name, default = TRUE)

3 messages · Bert Gunter, Leonard Mada

#
Dear R-Users,

I want to change the args() function to return by default the arguments 
of the default generic function:
args = function(name, default = TRUE) {
 ?? ?# TODO: && is.function.generic();
 ?? ?if(default) {
 ?? ???? fn = match.call()[[2]];
 ?? ???? fn = paste0(as.character(fn), ".default");
 ?? ???? name = fn;
 ?? ?}
 ?? ?.Internal(args(name));
}

Is there a nice way to find out if a function is generic: something like 
is.function.generic()?

Many thanks,

Leonard
=======

Note:
- the latest version of this code will be on GitHub:
https://github.com/discoleo/R/commits/master/Stat/Tools.Code.R
#
?.S3methods

f <- function()(2)
[1] 0
[1] 206

There may be better ways, but this is what came to my mind.
-- Bert

On Wed, Mar 8, 2023 at 11:09?AM Leonard Mada via R-help <
r-help at r-project.org> wrote:

            

  
  
#
Dear Bert,

Thank you for the idea.

It works, although a little bit ugly. The original code generated an 
ugly warning as well. I have modified it slightly:

is.function.generic = function(name) {
 ?? ?# TODO: is.function.generic();
 ?? ?# - this version is a little bit ugly;
 ?? ?# - S4: if(isGeneric(name));
 ?? ?length(do.call(.S3methods, list(name))) > 0;
}

The latest code is on GitHub:
https://github.com/discoleo/R/blob/master/Stat/Tools.Code.R

Sincerely,

Leonard

### initial variant:

is.function.generic = function(name) {
 ?? ?length(.S3methods(name)) > 0;
}
is.function.generic(plot)
# [1] TRUE
# Warning message:
# In .S3methods(name) :
#? generic function 'name' dispatches methods for generic 'plot'
On 3/8/2023 9:24 PM, Bert Gunter wrote: