How to examine the code of a function in R that's object class sensitive

function, r

Solution

When you say

the function did do other things depending on the class of the object thrown at it

you are already at the heart of the S3 dispatch mechanism! So I would recommend reading a programming book on R as e.g.

- (classic but dated) Venables/Ripley "S Programming",

- Gentleman "Bioinformatics with R",

- Brown/Murdoch "First Course in Statistical Programming with R",

- Chambers "Software for Data Analysis: Programming with R",

or other resources from this SO question on R books along with an example package or two from the rich set of CRAN packages.

Problem

I'm trying to write a function to do a particular job (in my case, analyse a data set for outliers) so the first things I want to do is look at how other people have done similar jobs. I can do this to load a particular package and examine the code of a function, but some functions seem to depend on what class of object you throw at it ``` >library(outliers) > fix(outlier) function (x, opposite = FALSE, logical = FALSE) { if (is.matrix(x)) apply(x, 2, outlier, opposite = opposite, logical = logical) else if (is.data.frame(x)) sapply(x, outlier, opposite = opposite, logical = logical) else { if (xor(((max(x) - mean(x)) < (mean(x) - min(x))), opposite)) { if (!logical) min(x) else x == min(x) } else { if (!logical) max(x) else x == max(x) } } } ``` How can you look at the code of something that changes depending on the object ? Edit: OK, Palm <- face. The function I used as an example just calls itself, but allt he code is there... I have seen other examples (but can't think of any offhand) where the function did do other things depending on the class of the object thrown at it, so the question stands, even though it's a bad example !

Original source