Debian Codegraph @

R/annotation.R

raw
1 ## annotation ###########################################################################
2
3 setMethod("annotation","ANY",function(x)attr(x,"annotation"))
4 setMethod("annotation","item",function(x)x@annotation)
5
6 setMethod("annotation<-",signature(x="ANY",value="NULL"),function(x,value){
7 attr(x,"annotation") <- NULL
8 x
9 })
10
11
12 setMethod("annotation<-",signature(x="ANY",value="character"),function(x,value){
13 value <- new("annotation",structure(as.character(value),names=names(value)))
14 callGeneric(x,value)
15 })
16
17
18 setMethod("annotation<-",signature(x="item",value="annotation"),function(x,value){
19 x@annotation <- value
20 x
21 })
22
23 setMethod("annotation<-",signature(x="ANY",value="annotation"),function(x,value){
24 attr(x,"annotation") <- value
25 x
26 })
27
28 description <- function(x){
29 d <- annotation(x)["description"]
30 if(!length(d)) return(NULL)
31 if(is.na(d)) NULL else unname(d)
32 }
33 setGeneric("description",function(x)standardGeneric("description"))
34
35 description.data.frame <- function(x){
36 dict <- lapply(x,attr,"label")
37 structure(dict,class="descriptions")
38 }
39
40 setMethod("description","data.frame",description.data.frame)
41 setMethod("description","tbl_df",description.data.frame)
42
43 "description<-" <- function(x,value){
44 annotation(x)["description"] <- value
45 x
46 }
47
48 wording <- function(x){
49 wdng <- annotation(x)["wording"]
50 if(is.na(wdng)) NULL else unname(wdng)
51 }
52
53 "wording<-" <- function(x,value){
54 annotation(x)["wording"] <- value
55 x
56 }
57
58 setMethod("show","annotation",function(object){
59 if(length(object)){
60 annot.out <- character()
61 for(i in seq_len(length(object))){
62 annot.i <- object[i]
63 nm.i <- trimws(names(annot.i))
64 annot.i <- strwrap(annot.i,width=getOption("width")-8)
65 annot.i <- c(paste(" ",annot.i),"")
66 if(nzchar(nm.i)){
67 annot.i <- c(
68 paste(nm.i,":",sep=""),
69 annot.i
70 )
71 }
72 annot.out <- c(annot.out,annot.i)
73 }
74 writeLines(annot.out)
75 }
76 })
77