For documentation we use a system that generates Rd files from special
comments in the code. (inlinedocs).
It is crucial to be able to get the defining source code for objects like
methods to extract the comments from it.
Here is an R session that shows how this works for several kinds of methods
and (at the end of the session) how if fails for methods of "initialize"
require("methods")
setGeneric( "myGen",function(arg){standardGeneric("myGen")})
[1] "myGen"
setMethod(
+ f="myGen",
+ signature="numeric",
+ definition=function # a function with comments in its source
+ ### that are used to document it with inlinedocs
+ (arg ##<< another special comment for the argument
+ ){
+ 2*arg
+ ### a description for the return value
+ }
+ )
[1] "myGen"
we can get the whole function definition with comments back
by the following snippet:
attr(getMethod("myGen","numeric"),"srcref")
function # a function with comments in its source
### that are used to document it with inlinedocs
(arg ##<< another special comment for the argument
){
2*arg
### a description for the return value
}
this also works for operators
setMethod("$",
+ signature(x = "MyClass"),
+ function
+ (x, ##<< first arg
+ name ##<< second ag
+ ) { }
+ )
[1] "$"
attr(getMethod("$","MyClass"),"srcref")
function
(x, ##<< first arg
name ##<< second ag
) { }
setClass(
+ Class="MyClass",
+ representation(
+ val="numeric"
+ )
+ )
It works also for other functions already defined:
setGeneric("plot")
[1] "plot"
setMethod("plot",
+ signature(x = "MyClass"),
+ function # a comment
+ (x, y, ...)
+ {
+ stop("need a definition for the method here")
+ }
+ )
[1] "plot"
attr(getMethod("plot","MyClass"),"srcref")
function # a comment
(x, y, ...)
{
stop("need a definition for the method here")
}
However if we overload initialize there is no "srcref" attribute:
setMethod(
+ f="initialize",
+ signature="MyClass",
+ definition=function # here are also comments but we can not retrieve
them
+ (.Object,value){
+ .Object@val<- value
+ return(.Object)
+ }
+ )
[1] "initialize"
attr(getMethod("initialize","MyClass"),"srcref")
NULL
Is there a reason for this behavior, and more important still,
Ist there a way to get at the source of my constructors to document them
automatically?