attr and comment - was Re: [R] self documenting workspaces

John Aitchison jaitchis at lisp.com.au
Tue Feb 20 01:19:58 CET 2001


Hi

The suggestions below were to use attr()  and comment() ..
it seems from my tests that neither of these survive edit or fix , 
likewise attributes(). This is true of functions, but not of vectors.

---------------------------------
eg this works fine

> x<-1:10
> comment(x) <- 'this is a comment'
> attr(x,'creation')<-date()
> fix(x)
> x
[1]  1  9 10
attr(,"creation")
[1] "Mon Feb 19 23:55:55 2001"
> attributes(x)
$comment
[1] "this is a comment"
$creation
[1] "Mon Feb 19 23:55:55 2001"
----------------------------------------'
but this does not
 f<-function(x){attributes(x)}
> comment(f) <- 'function comment'
> attr(f,'creation')<-date()
> attributes(f)
$source
[1] "function(x){attributes(x)}"
$comment
[1] "function comment"
$creation
[1] "Tue Feb 20 00:00:15 2001"

> fix(f)
> attributes(f)
$source
[1] "function(x){print(attributes(x))" "}"                               
-------------------------------------------------------------

Is this a feature, a bug, an oversight? ??


It is easily 'fixed' apparently with

myfix<-function(f){s<-attributes(f);fix(f);attributes(f)<-s}

I cannot call myfix 'fix' here (ie override fix) because of infinite 
recursion .. what I am missing notation wise? (ie to call the 'ancestor'
of fix)

fix<-function(f){s<-attributes(f);base:fix(f);attributes(f)<-s} ? or 
somesuch

thanks again



On 19 Feb 2001, at 18:23, Peter Dalgaard BSA wrote:

> Thomas Lumley <tlumley at u.washington.edu> writes:
> 
> > YOu could attach a "doc" attribute to everything. This has the
> > disadvantage that for objects with no special "print" method the
> > attribute would always be printed with the object.
> > 
> > attr(a,"doc")<-"A is for apple"
> > 
> > and you could write a function that listed objects and documentation
> > using something like sapply(ls(),function(x) attr(x,"doc")
> 
> Actually, once upon a time in the far East, Ross wrote comment(x) and
> comment(x)<- to do just that. We're not very good at keeping them,
> though - for instance, they don't survive a fix() of the object.
> 

John Aitchison
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list