Debian Codegraph @

R/view.R

raw
1 view <- function(x,
2 title=deparse(substitute(x)),
3 vfunc=getOption("vfunc","View"),
4 ...)
5 {
6
7 # cls <- class(x)
8 # title <- paste0(cls,": ",title)
9
10 prepd <- viewPrep(x,title,...)
11 title <- attr(prepd,"title")
12
13 View.call <- call(vfunc,x=prepd,title=title)
14 eval(View.call,globalenv())
15 }
16
17 viewPrep <- function(x,title,...)UseMethod("viewPrep")
18 viewPrep.default <- function(x,title,...){
19 return(structure(x,title=title))
20 }
21
22 viewPrep.data.set <- function(x,title,...){
23 # title <- paste("Data set:",title)
24 Data <- lapply([email protected],format,justify="left")
25 frame <- structure(Data,
26 row.names=x@row_names,
27 names=x@names,
28 class="data.frame")
29 for(n in names(frame)){
30 d <- description(x[[n]])
31 if(length(d))
32 attr(frame[[n]],"label") <- d
33 }
34 return(structure(frame,title=title))
35 }
36
37 viewPrep.data.frame <- function(x,title,...){
38 structure(x,title=title)
39 }
40
41 viewPrep.descriptions <- function(x,title,...){
42 # title <- paste("Descriptions:",title)
43 viewPrep.data.frame(as.data.frame(x),
44 title=title,
45 ...)
46 }
47
48 parse_string <- function(x)parse(file=NULL,text=x)
49 format_lab_view <- function(x){
50 fmt <- paste(unname(x),sQuote(names(x)))
51 fmt <- paste(fmt,collapse=", ")
52 return(fmt)
53 }
54 in_range <- function(x,rng){
55 x >= rng[1] & x <= rng[2]
56 }
57
58 view1cp <- function(x){
59 n <- x["name"]
60 d <- x["description"]
61 l <- x["labels"]
62 if(nzchar(l))
63 l <- eval(parse(text=l))
64 else
65 l <- NULL
66 ms <- x["measurement"]
67 vf <- x["value.filter"]
68 if(nzchar(vf)){
69 vf <- paste0("list(",vf,")")
70 vf <- eval(parse(text=vf))
71 cl <- names(vf)
72 vf <- vf[[1]]
73 if(cl=="missing")
74 vf <- new("missing.values",
75 filter=vf$values,
76 range=vf$range
77 )
78 else if(cl=="valid"){
79 if(length(vf$values))
80 vf <- new("valid.values",filter=vf$values)
81 else if(length(vf$range))
82 vf <- new("valid.range",filter=vf$range)
83 }
84 } else vf <- NULL
85 nl <- length(l)
86 tab <- matrix("",ncol=4,nrow=max(1,nl))
87 colnames(tab) <- c("variable","description","value","label")
88 tab[1,1] <- n
89 tab[1,2] <- d
90 omtab <- NULL
91 if(nl>0){
92 if(length(vf))
93 ism <- is.missing2(l,vf)
94 else
95 ism <- FALSE
96 tab[1:nl,3] <- paste(unname(l),ifelse(ism,"M"," "))
97 tab[1:nl,4] <- names(l)
98 if(length(vf) && inherits(vf,"missing.values")){
99 if(length(vf@filter) && any(vf@filter %nin% l)){
100 omiss <- setdiff(vf@filter,l)
101 omiss <- paste(omiss,"M")
102 omtab <- rbind(omtab,cbind("","",omiss,"(unlabelled)"))
103 }
104 # if(length(vf@range) && any(!in_range(l,vf@range))){
105 # omiss <- paste(vf@range[1],"--",vf@range[2],"M")
106 # omtab <- rbind(omtab,cbind("","",omiss,"(unlabelled)"))
107 # }
108 }
109 } else if(length(vf)){
110 if(inherits(vf,"missing.values")){
111 if(length(vf@filter)){
112 omiss <- setdiff(vf@filter,l)
113 omiss <- paste(omiss,"M")
114 omtab <- rbind(omtab,cbind("","",omiss,"(unlabelled)"))
115 }
116 if(length(vf@range)){
117 omiss <- paste(vf@range[1],"--",vf@range[2],"M")
118 omtab <- rbind(omtab,cbind("","",omiss,"(unlabelled)"))
119 }
120 } else if(inherits(vf,"valid.values")){
121 nvd <- length(vf@filter)
122 if(nvd > 1)
123 tab <- rbind(tab,matrix("",ncol=ncol(tab),nrow=nvd-1))
124 tab[1:nvd,3] <- paste(vf@filter," ")
125 tab[1:nvd,4] <- "(unlabelled)"
126 } else if(inherits(vf,"valid.range")){
127 tab[1,3] <- paste(vf@filter[1],"--",vf@filter[2]," ")
128 tab[1:nvd,4] <- "(unlabelled)"
129 }
130 }
131
132 if(length(omtab)){
133 tab <- rbind(tab,omtab)
134 }
135 return(tab)
136 }
137
138 viewPrep.codeplan <- function(x,title,compact=FALSE,...){
139 # title <- paste("Code plan:",title)
140 if(compact){
141 labels <- x$labels
142 nll <- nzchar(labels)
143 labels <- as.list(labels)
144 labels[nll] <- lapply(labels[nll],parse_string)
145 labels[nll] <- lapply(labels[nll],eval)
146 labels[nll] <- lapply(labels[nll],format_lab_view)
147 labels <- unlist(labels)
148 frame <- data.frame(variable=x$name,
149 description=x$description,
150 labels=labels,
151 value.filter=x$value.filter)
152 viewPrep.data.frame(frame,
153 title=title,
154 ...)
155 }
156 else {
157 if(nrow(x)>1){
158 tabs <- apply(x,1,view1cp)
159 tab <- do.call(rbind,tabs)
160 }
161 else
162 tab <- view1cp(unlist(x))
163
164 tab[,3] <- format(trimws(tab[,3]),justify="right",width=5)
165 viewPrep.default(tab,
166 title=title,
167 ...)
168 }
169 }
170
171 viewPrep.importer <- function(x,title,compact=TRUE,...){
172 cp <- codeplan(x)
173 viewPrep.codeplan(x=cp,title=title,compact=compact,...)
174 }