Debian Codegraph @

R/spss-syntax-methods.R

raw
1 spss.fixed.file <- function(
2 file,
3 columns.file,
4 varlab.file=NULL,
5 codes.file=NULL,
6 missval.file=NULL,
7 count.cases=TRUE,
8 to.lower=getOption("spss.fixed.to.lower",FALSE),
9 iconv=TRUE,
10 encoded=getOption("spss.fixed.encoding","cp1252"),
11 negative2missing = FALSE
12 ){
13 file <- force(file)
14 columns.file <- force(columns.file)
15 file <- path.expand(file)
16 columns.file <- path.expand(columns.file)
17 check.file(file,error=TRUE)
18 fptr <- rofile(file)
19 check.file(columns.file,error=TRUE)
20 data.spec <- spss.parse.data.spec(columns.file)
21 types <- data.spec$types
22 # browser()
23 varlabs <- if(length(varlab.file) && check.file(varlab.file,error=TRUE))
24 spss.parse.variable.labels(varlab.file,
25 iconv=iconv,
26 encoded=encoded)
27 else NULL #vector(length(types),mode="list")
28 vallabs <- if(length(codes.file) && check.file(codes.file,error=TRUE))
29 spss.parse.value.labels(codes.file,
30 iconv=iconv,
31 encoded=encoded)
32 else NULL #vector(length(types),mode="list")
33 missings <- if(length(missval.file) && check.file(missval.file,error=TRUE)) spss.parse.missing.values(missval.file)
34 else NULL #vector(length(types),mode="list")
35 variables <- vector(length(types),mode="list")
36
37 var.names <- names(types)
38 names(variables) <- var.names
39 variables[types==1] <- list(new("double.item"))
40 variables[types==2] <- list(new("character.item"))
41
42 if(length(varlabs)){
43 nn <- names(varlabs)
44 if(!all(nn %in% var.names)) stop("undefined variables in 'varlab.file'")
45 for(n in nn)
46 description(variables[[n]]) <- varlabs[n]
47 }
48 if(length(vallabs)){
49 nn <- names(vallabs)
50 if(!all(nn %in% var.names)) stop("undefined variables in 'codes.file'")
51 for(n in nn)
52 suppressWarnings(labels(variables[[n]]) <- vallabs[[n]])
53 }
54 if(length(missings)){
55 nn <- names(missings)
56 if(!all(nn %in% var.names)) stop("undefined variables in 'missval.file'")
57 for(n in nn)
58 missing.values(variables[[n]]) <- missings[[n]]
59 }
60 if(negative2missing)
61 variables <- lapply(variables,`valid.range<-`,c(0,Inf))
62
63 nlines <- if(count.cases) {
64 rofseek(fptr,pos=0)
65 for(i in seq_len(data.spec$skip)) roreadline(fptr)
66 nlines <- .Call("countlines",fptr)
67 } else NA_integer_
68 attr(fptr,"nlines") <- nlines
69
70 if(to.lower){
71 names(variables) <- tolower(names(variables))
72 }
73
74 if(iconv)
75 variables <- lapply(variables,Iconv,from=encoded,to="UTF-8")
76 else
77 encoded = ""
78
79 warn_if_duplicate_labels(variables)
80
81 new("spss.fixed.importer",
82 variables,
83 ptr=fptr,
84 columns.file=columns.file,
85 varlab.file=varlab.file,
86 codes.file=codes.file,
87 missval.file=missval.file,
88 data.spec=data.spec
89 )
90 }
91 setMethod("initialize","spss.fixed.importer",function(.Object,
92 variables,
93 ptr,
94 columns.file=character(),
95 varlab.file=character(),
96 codes.file=character(),
97 missval.file=character(),
98 document=character(),
99 data.spec
100 ){
101 [email protected] <- variables
102 .Object@ptr <- ptr
103 [email protected] <- as.character(columns.file)
104 [email protected] <- as.character(varlab.file)
105 [email protected] <- as.character(codes.file)
106 [email protected] <- as.character(missval.file)
107 .Object@document <- as.character(document)
108 [email protected] <- data.spec
109 .Object
110 })
111
112
113 setMethod("getNobs","spss.fixed.importer",function(x){
114 nlines <- attr(x@ptr,"nlines")
115 if(!length(nlines)) {
116 rofseek(x@ptr,pos=0)
117 for(i in seq_len([email protected]$skip)) roreadline(x@ptr)
118 attr(x@ptr,"nlines") <- nlines <- .Call("countlines",x@ptr)
119 }
120 nlines
121 })
122
123 setMethod("seekData","spss.fixed.importer",function(x){
124 rofseek(x@ptr,pos=0)
125 for(i in seq_len([email protected]$skip))
126 roreadline(x@ptr)
127 })
128
129 setMethod("readData","spss.fixed.importer",
130 function(x,n)
131 readfixed(x@ptr,
132 what=x,
133 nlines=n,
134 [email protected]$start,
135 [email protected]$stop
136 ))
137
138 setMethod("readSlice","spss.fixed.importer",
139 function(x,rows,cols)
140 readfixedslice(x@ptr,
141 what=x,
142 j=cols,i=rows,
143 [email protected]$start,
144 [email protected]$stop
145 ))
146
147 setMethod("readChunk","spss.fixed.importer",
148 function(x,nrows,cols)
149 readfixedchunk(x@ptr,
150 what=x,
151 j=cols,n=nrows,
152 [email protected]$start,
153 [email protected]$stop
154 ))
155
156 setMethod("show","spss.fixed.importer",
157 function(object){
158 file.name <- attr(object@ptr,"file.name")
159 nobs <- nrow(object)
160 nvar <- ncol(object)
161 varlab.file <- [email protected]
162 codes.file <- [email protected]
163 missval.file <- [email protected]
164 cat("\nSPSS fixed column file",sQuote(file.name),"\n\twith ")
165 cat(nvar,"variables and ")
166 cat(nobs,"observations\n")
167 if(length(varlab.file)) cat("\twith variable labels from file",sQuote(varlab.file),"\n")
168 if(length(codes.file)) cat("\twith value labels from file",sQuote(codes.file),"\n")
169 if(length(missval.file)) cat("\twith missing value definitions from file",sQuote(missval.file),"\n")
170 })
171
172 subset.spss.fixed.importer <- subset.importer