funWhich: get the dispatched S3 or S4 method of a function

View source: R/functionInfo.R

funWhichR Documentation

get the dispatched S3 or S4 method of a function

Description

get the dispatched S3 or S4 method of a function

Usage

funWhich(f, object, returnAll = FALSE, envir = topenv(parent.frame()))

Arguments

f

function or function name.

object

the object that the function f is processing.

returnAll

Whether all possibly existed original functions S3 method and S4 method are returned as a list. The default is FALSE, meaning returning S3 method first if exists, followed by S4 method if exists while S3 method not exists. If both S3 and S4 methods do not exist, original function appears to be not a generic function so the original source is returned.

envir

the working environment.

Value

either a list of length 3, containing original function, S3 method and S4 method, or a function object.

Examples

## Not run: 
# print from base package is a S3 generic
detachPackages("Matrix")
funWhich(print, "asdfasdfasd", TRUE)
funWhich(print, "asdfasdfasd")

library(Matrix) # print becomes a S4 standardGeneric
funWhich(print, "asdfasdfasd", TRUE)
funWhich(print, "asdfasdfasd") # if S3 and S4 both exists, then S3 is shown.

fit <- lm(Sepal.Length ~ Petal.Length + Petal.Width + Species, 
          data = datasets::iris)
resFun = funWhich(print, fit)
resFun
resFun(fit)
stats:::print.lm(fit)

funWhich(show, object = ~ a + b, T)
funWhich(show, object = ~ a + b)
funWhich(showDefault, object = ~ a + b)

x = show
funWhich(x, object = ~ a + b, T)
funWhich(x, object = ~ a + b)
xw = funWhich("x", object = ~ a + b)
xw
attributes(xw)

## End(Not run)

paodan/funcTools documentation built on April 1, 2024, 12:01 a.m.