Debian Codegraph @

R/applyTemplate.R

raw
1 allequal <- function(x) length(unique(x)) == 1
2
3 get.substr <- function(pattern, text, perl = FALSE,
4 fixed = FALSE, useBytes = FALSE){
5 if(fixed)
6 rr <- gregexpr(pattern,text,fixed=TRUE,perl=FALSE,useBytes=useBytes)
7 else
8 rr <- gregexpr(pattern,text,perl=perl,useBytes=useBytes)
9 positions <- lapply(rr,function(rr){
10 if(rr[1]<0) return(NULL)
11 len <- attr(rr,"match.length")
12 sapply(1:length(rr),function(i){
13 start <- rr[i]
14 stop <- rr[i] + len[i] - 1
15 c(start=start,stop=stop)
16 })})
17 ans <- lapply(1:length(text),function(i){
18 txt <- text[i]
19 pos <- positions[[i]]
20 if(!length(pos)) return(NULL)
21 sapply(1:NCOL(pos),function(j){
22 substr(txt,start=pos["start",j],stop=pos["stop",j])
23 })
24 })
25 structure(ans,positions=positions)
26 }
27
28 get.oneSubstr <- function(pattern,text){
29 if(length(text)>1) {
30 warning("only first element used")
31 text <- text[1]
32 }
33 start <- regexpr(pattern,text)
34 if(start < 0) return(NULL)
35 stop <- start + attr(start,"match.length") - 1
36 substr(text,start=start,stop=stop)
37 }
38
39
40 formatSigSymbols <- function(x,signif.symbols=getOption("signif.symbols")){
41 if(length(x)>1) {
42 warning("only first element used")
43 x <- x[1]
44 }
45 signif.symbols <- sort(signif.symbols)
46 sel <- x <= signif.symbols
47 if(!any(sel)) return("")
48 sel <- which(signif.symbols == min(signif.symbols[sel]))
49 return(names(signif.symbols)[sel])
50 }
51
52 formatOne <- function(x,spec,format="f",
53 digits=min(3,getOption("digits")),
54 signif.symbols=getOption("signif.symbols")
55 ){
56 if(length(x)>1) {
57 warning("only first element used")
58 x <- x[1]
59 }
60 if(is.na(x)) return("")
61 if(length(spec)>1) {
62 warning("only first spec used")
63 spec <- spec[1]
64 }
65 if(is.character(x)) return(x)
66 if(!is.numeric(x)) {
67 x <- paste(x,collapse=" ")
68 if(nchar(x)>20)
69 x <- paste(substr(x,1,20),"...")
70 return(x)
71 }
72 format.arg <- format
73 digits.arg <- digits
74 format <- get.oneSubstr("[d-gEG]+",spec)
75 digits <- as.numeric(get.oneSubstr("[0-9]+",spec))
76 hash <- length(get.oneSubstr("[#]",spec)) > 0
77 star <- length(get.oneSubstr("[*]",spec)) > 0
78 if(star) return(formatSigSymbols(x,signif.symbols))
79 if(hash){
80 if(length(digits))
81 digits <- min(digits,digits.arg)
82 else
83 digits <- digits.arg
84 }
85 if(!length(digits)) digits <- digits.arg
86 if(!length(format)) format <- format.arg
87 if(!(format %in% c("d","e","f","g","fg","E","G","s"))) stop("illegal format specifier")
88 return(formatC(x,digits=digits,format=format,width=-1))
89 }
90
91 formatVec <- function(x,formats,
92 default="f",
93 digits=min(3,getOption("digits")),
94 signif.symbols=getOption("signif.symbols")){
95 i <- 1:length(formats)
96
97 res <- sapply(i,function(i) if(length(formats[[i]]))
98 sapply(formats[[i]],formatOne,
99 x=x,
100 format=default,
101 digits=digits,
102 signif.symbols = signif.symbols)
103 else NULL
104 )
105 }
106
107 get.format <- function(x){
108 if(!length(x)) return(NULL)
109 x <- strsplit(x,":",fixed=TRUE)
110 sapply(x,function(x)sub(")","",x[2],fixed=TRUE))
111 }
112
113
114 applyTemplate <- function(x,template,
115 float.style=getOption("float.style"),
116 digits=min(3,getOption("digits")),
117 signif.symbols=getOption("signif.symbols")){
118 if(is.list(x)){
119 y <- list()
120 nms <- names(x)
121 for(i in seq_along(x)){
122 n_i <- nms[i]
123 if(n_i==""){
124 y[[i]] <- applyTemplate(x[[i]],
125 template=template,
126 float.style=float.style,
127 digits=digits,
128 signif.symbols=signif.symbols)
129 }
130 else {
131 if(n_i %in% names(template)){
132 tmpl <- template[n_i]
133 tmpl <- sub(n_i,"1",tmpl)
134 y[[i]] <-as.matrix(unlist(lapply(x[[i]],
135 applyTemplate,
136 template=unname(tmpl),
137 float.style=float.style,
138 digits=digits,
139 signif.symbols=signif.symbols)))
140 }
141 }
142 }
143 do.call(rbind,y)
144 }
145 else {
146 applyTemplate1(x, template=template,
147 float.style=float.style,
148 digits=digits,
149 signif.symbols=signif.symbols)
150 }
151 }
152
153 applyTemplate1 <- function(x,template,float.style=getOption("float.style"),
154 digits=min(3,getOption("digits")),
155 signif.symbols=getOption("signif.symbols")){
156 template <- as.matrix(template)
157 if(is.numeric(x))
158 patterns <- paste("\\(\\$",1:length(x),":[d-gEG]*[0-9]*[#]?[*]?\\)",sep="")
159 else
160 patterns <- paste("\\(\\$",1:length(x),"\\)",sep="")
161 names(patterns) <- as.character(1:length(x))
162 if(length(names(x))){
163 if(is.numeric(x))
164 npatterns <- paste("\\(\\$",names(x),":[d-gEG]*[0-9]*[#]?[*]?\\)",sep="")
165 else
166 npatterns <- paste("\\(\\$",names(x),"\\)",sep="")
167 names(npatterns) <- names(x)
168 }
169 else npatterns <- c()
170 res <- template
171 for(i in 1:length(patterns)){
172 targets <- get.substr(patterns[i],template)
173 positions <- attr(targets,"positions")
174 formats <- lapply(targets,get.format)
175 formatted <- formatVec(x[i],formats,
176 default=float.style,
177 digits=digits,
178 signif.symbols=signif.symbols)
179 for(j in 1:length(template)){
180 if(length(targets[[j]])){
181 targets.j <- targets[[j]]
182 formatted.j <- formatted[[j]]
183 for(k in 1:length(targets.j))
184 res <- gsub(targets.j[k],formatted.j[k],res,fixed=TRUE)
185 }
186 }
187 }
188 if(length(npatterns)){
189 for(n in names(npatterns)){
190 targets <- get.substr(npatterns[n],template)
191 positions <- attr(targets,"positions")
192 formats <- lapply(targets,get.format)
193 formatted <- formatVec(x[n],
194 formats,
195 default=float.style,
196 digits=digits,
197 signif.symbols=signif.symbols)
198 for(j in 1:length(template)){
199 if(length(targets[[j]])){
200 targets.j <- targets[[j]]
201 formatted.j <- formatted[[j]]
202 for(k in 1:length(targets.j))
203 res <- gsub(targets.j[k],formatted.j[k],res,fixed=TRUE)
204 }
205 }
206 }
207 }
208 unchanged <- res == template
209 res[unchanged] <- ""
210 res
211 }
212