[R-pkg-devel] Concise Summary For Any Variety Of R Function

Simon Urbanek @|mon@urb@nek @end|ng |rom R-project@org
Tue Oct 19 11:12:33 CEST 2021


Dario,

there is not such thing as S4 function. However, all functions have an environment and for functions from packages that environment will be the namespace of the package. So in those special cases you can use environmentName() to get the name, e.g.:

> who = function(f) cat(deparse(substitute(f)), "from", environmentName(environment(f)),"\n")
> who(ls)
ls from base 
> who(rnorm)
rnorm from stats 
> who(who)
who from R_GlobalEnv 

However, this won't work for functions defined in arbitrary environments which have no name:

> local({ f= function(x) x
+ who(f)
+ })
f from  

Depending on how much time you want to spend, you can do a search up the frames to find a named environment, but the concept you're trying to implement looks very fragile.

Cheers,
Simon



> On Oct 19, 2021, at 9:00 PM, Dario Strbenac <dstr7320 using uni.sydney.edu.au> wrote:
> 
> Good day,
> 
> I would like to create a concise show method for an S4 class that I am developing. One class slot stores a function specified by the end user. It could be a basic function, an S3 function, an S4 function. For S4 functions, I can get a concise representation:
> 
>> capture.output(showMethods(limmaSelection))[1]
> [1] "Function: limmaSelection (package ClassifyR)"
> 
> If the user specified the function bdiag from Matrix, how could I generalise the show code to describe the function, such that the output would be "Function bdiag from package Matrix" and simiarly for an S3 function?
> 
> --------------------------------------
> Dario Strbenac
> University of Sydney
> Camperdown NSW 2050
> Australia
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
> 



More information about the R-package-devel mailing list