Debian Codegraph @

R/codeplan.R

raw
1 setMethod("codeplan",
2 signature(x="ANY"),function(x) NULL)
3
4 setMethod("codeplan",
5 signature(x="item.list"),
6 function(x){
7 dat <- [email protected]
8 n <- x@names
9 cp <- lapply(dat,codeplan1)
10 names(cp) <- n
11 structure(cp,
12 class="codeplan")
13 })
14
15 setMethod("codeplan",
16 signature(x="item"),
17 function(x){
18 cp <- codeplan1(x)
19 structure(list(cp),class="codeplan")
20 })
21
22 codeplan1 <- function(x) {
23 l <- list(
24 annotation=depr_annot(x),
25 labels=depr_labels(x),
26 value.filter=depr_vfilter(x),
27 mode=mode(x),
28 measurement=measurement(x)
29 )
30 l[sapply(l,length)>0]
31 }
32
33 print.codeplan <- function(x,...){
34 cat("\n",as.yaml(unclass(x)),"\n",sep="")
35 }
36
37 any_nzchar <- function(x) any(nzchar(x))
38
39
40 formatw <- function(x,width){
41 l <- nchar(x)
42 to.abbr <- l > width
43 x[to.abbr] <- paste0(substr(x[to.abbr],1,width-3),"...")
44 format(x)
45 }
46
47 depr_annot <- function(x){
48 a <- annotation(x)
49 a <- structure([email protected], names=names(a))
50 if(length(a)) as.list(a)
51 else NULL
52 }
53
54 depr_labels <- function(x){
55 l <- labels(x)
56 if(length(l))
57 structure(as.list(l@values),[email protected])
58 else NULL
59 }
60
61 depr_vfilter <- function(x){
62 v <- value.filter(x)
63 if(length(v)){
64 cl <- as.character(class(v))
65 if(cl == "missing.values"){
66 res <- list(class=cl,
67 range=v@range,
68 values=v@filter)
69 }
70 else if(cl == "valid.values"){
71 res <- list(class=cl,
72 values=v@filter)
73 }
74 else if(cl == "valid.range"){
75 res <- list(class=cl,
76 range=v@filter)
77 }
78 else res <- NULL
79 res <- res[sapply(res,length)>0]
80 }
81 else NULL
82 }
83
84 setMethod("setCodeplan",signature(x="data.frame",value="codeplan"),
85 function(x,value){
86 x <- as.data.set(x)
87 setCodeplan(x,value)
88 })
89
90 setMethod("setCodeplan",signature(x="data.frame",value="NULL"),
91 function(x,value) x)
92
93 setMethod("setCodeplan",signature(x="data.set",value="codeplan"),
94 function(x,value){
95 n <- intersect(names(x),names(value))
96 if(length(n))
97 for(nn in n){
98 x[[nn]] <- setCodeplan1(x[[nn]],value[[nn]])
99 }
100 x
101 })
102
103 setMethod("setCodeplan",signature(x="data.set",value="NULL"),
104 function(x,value){
105 y <- as.list(x)
106 y <- lapply(y,setCodeplan,value=NULL)
107 as.data.frame(y,row.names=row.names(x))
108 })
109
110 setMethod("setCodeplan",signature(x="item",value="codeplan"),
111 function(x,value){
112 setCodeplan1(x,value[[1]])
113 })
114
115 setMethod("setCodeplan",signature(x="atomic",value="codeplan"),
116 function(x,value){
117 setCodeplan1(x,value[[1]])
118 })
119
120 setMethod("setCodeplan",signature(x="item",value="NULL"),
121 function(x,value){
122 [email protected]
123 })
124
125 setMethod("setCodeplan",signature(x="atomic",value="NULL"),
126 function(x,value){
127 [email protected]
128 })
129
130
131
132 "codeplan<-" <- function(x,value) {
133 x <- setCodeplan(x,value)
134 invisible(x)
135 }
136
137 setCodeplan1 <- function(x,val){
138
139 if(is.null(x)){
140 x <- vector(mode=val$mode)
141 }
142 else if(mode(x) != val$mode)
143 stop(sprintf("mode conflict: '%s' != '%s'",
144 mode(x),val$mode))
145 l <- val$labels
146 if(length(l)){
147 l <- unlist(l)
148 labels(x) <- l
149 }
150 m <- val$measurement
151 measurement(x) <- m
152 a <- val$annotation
153 if(length(a)){
154 annotation(x) <- unlist(a)
155 }
156 vf <- val$value.filter
157 if(length(vf)){
158 cl <- vf$class
159 if(cl=="missing.values")
160 vf <- new(cl,
161 filter=vf$values,
162 range=vf$range
163 )
164 else if(cl=="valid.values")
165 vf <- new(cl,filter=vf$values)
166 else if(cl=="valid.range")
167 vf <- new(cl,filter=vf$range)
168 [email protected] <- vf
169 }
170 x
171 }
172
173 write_codeplan <- function(x,filename,
174 type=NULL,
175 pretty=FALSE){
176 if(!length(type)){
177 if(endsWith(filename,".yaml") ||
178 endsWith(filename,".yml"))
179 type <- "yaml"
180 else if(endsWith(filename,".json"))
181 type <- "json"
182 }
183
184 if(type=="yaml"){
185 write_yaml(unclass(x),file=filename)
186 }
187 else if(type=="json"){
188 write_json(unclass(x),path=filename,
189 auto_unbox=TRUE,pretty=pretty)
190 }
191 else stop(sprintf("File type %s not supported (yet)",type))
192 }
193
194 read_codeplan <- function(filename,
195 type=NULL){
196 if(!length(type)){
197 if(endsWith(filename,".yaml") ||
198 endsWith(filename,".yml"))
199 type <- "yaml"
200 else if(endsWith(filename,".json"))
201 type <- "json"
202 }
203
204 if(type=="yaml"){
205 structure(read_yaml(file=filename),class="codeplan")
206 }
207 else if(type=="json"){
208 structure(read_json(path=filename, simplifyVector = TRUE),
209 class="codeplan")
210 }
211 else stop(sprintf("File type %s not supported (yet)",type))
212 }