| 1 |
spss.file <- function(file,...){ |
| 2 |
if(grepl("\\.por$",file) || grepl("\\.POR$",file)) |
| 3 |
spss.portable.file(file,...) |
| 4 |
else |
| 5 |
spss.system.file(file,...) |
| 6 |
} |
| 7 |
|
| 8 |
spss.system.file <- function( |
| 9 |
file, |
| 10 |
varlab.file=NULL, |
| 11 |
codes.file=NULL, |
| 12 |
missval.file=NULL, |
| 13 |
count.cases=TRUE, |
| 14 |
to.lower=getOption("spss.sav.to.lower",FALSE), |
| 15 |
iconv=TRUE, |
| 16 |
encoded=getOption("spss.sav.encoding","cp1252"), |
| 17 |
ignore.scale.info = FALSE, |
| 18 |
negative2missing = FALSE |
| 19 |
){ |
| 20 |
file <- path.expand(file) |
| 21 |
check.file(file,error=TRUE) |
| 22 |
ptr <- .Call("NewSysFile",file) |
| 23 |
|
| 24 |
data.spec <- parseSysHeader(ptr) |
| 25 |
types <- data.spec$types |
| 26 |
|
| 27 |
varnames <- names(data.spec$variables) |
| 28 |
|
| 29 |
varlabs <- lapply(data.spec$variables,"[[",i="label") |
| 30 |
varlabs <- unlist(varlabs[sapply(varlabs,length)>0]) |
| 31 |
vallabs <- data.spec$value.labels |
| 32 |
vallabs.vars <- lapply(vallabs,"[[",i="variables") |
| 33 |
vallabs.vars <- lapply(vallabs.vars,function(i)names(types)[i]) |
| 34 |
vallab.tmp <- lapply(seq_along(vallabs),function(i){ |
| 35 |
ans <- list() |
| 36 |
ans[vallabs.vars[[i]]] <- list(vallabs[[i]]$labels) |
| 37 |
ans |
| 38 |
}) |
| 39 |
vallabs <- unlist(vallab.tmp,recursive=FALSE) |
| 40 |
names.vallabs <- names(vallabs) |
| 41 |
string.vallabs <- types[names.vallabs] > 0 |
| 42 |
vallabs[string.vallabs] <- lapply(vallabs[string.vallabs],spss.string.vallabs) |
| 43 |
|
| 44 |
missings <- mapply(function(v,r)list(values=v,range=r), |
| 45 |
data.spec$missings$values, |
| 46 |
data.spec$missings$ranges, |
| 47 |
SIMPLIFY=FALSE |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
missings <- lapply(missings,function(x)if(!length(x$values) & !length(x$range))NULL else x) |
| 53 |
missings <- missings[sapply(missings,length)>0] |
| 54 |
|
| 55 |
varprintfmt <- lapply(data.spec$variables,"[[",i="print") |
| 56 |
varprintfmt <- sapply(varprintfmt,"[",i=3) |
| 57 |
vardatetime <- varprintfmt %in% c(20,22:24,26:30,38,39) |
| 58 |
|
| 59 |
variables <- vector(length(types),mode="list") |
| 60 |
names(variables) <- names(types) |
| 61 |
variables[types==0] <- list(new("double.item")) |
| 62 |
variables[types>0] <- list(new("character.item")) |
| 63 |
variables[types<0] <- NULL |
| 64 |
|
| 65 |
variables[vardatetime] <- list(new("datetime.item", |
| 66 |
tzone="GMT", |
| 67 |
origin="1582-10-14")) |
| 68 |
|
| 69 |
if(length(varlab.file) && check.file(varlab.file,error=TRUE)){ |
| 70 |
message("using ",varlab.file) |
| 71 |
varlabs <- spss.parse.variable.labels(varlab.file, |
| 72 |
iconv=iconv, |
| 73 |
encoded=encoded) |
| 74 |
} |
| 75 |
if(length(codes.file) && check.file(codes.file,error=TRUE)){ |
| 76 |
message("using ",codes.file) |
| 77 |
vallabs <- spss.parse.value.labels(codes.file, |
| 78 |
iconv=iconv, |
| 79 |
encoded=encoded) |
| 80 |
} |
| 81 |
if(length(missval.file) && check.file(missval.file,error=TRUE)){ |
| 82 |
message("using ",missval.file) |
| 83 |
missings <- spss.parse.missing.values(missval.file) |
| 84 |
} |
| 85 |
|
| 86 |
if(length(varlabs)) |
| 87 |
variables[names(varlabs)] <- mapply("description<-",variables[names(varlabs)],varlabs) |
| 88 |
if(length(vallabs)){ |
| 89 |
unique_ <- function(x){ |
| 90 |
dup <- duplicated(x) |
| 91 |
x[!dup] |
| 92 |
} |
| 93 |
vallabs <- lapply(vallabs,unique_) |
| 94 |
suppressWarnings(variables[names(vallabs)] <- mapply("labels<-",variables[names(vallabs)],vallabs)) |
| 95 |
} |
| 96 |
if(length(missings)) |
| 97 |
variables[names(missings)] <- mapply("missing.values<-",variables[names(missings)],missings) |
| 98 |
if(negative2missing) |
| 99 |
variables <- lapply(variables,`valid.range<-`,c(0,Inf)) |
| 100 |
|
| 101 |
ncases <- data.spec$header$ncases |
| 102 |
if(ncases < 0){ |
| 103 |
if(count.cases){ |
| 104 |
.Call("rewind_sysfile",ptr) |
| 105 |
ncases <- .Call("count_cases_sysfile",ptr) |
| 106 |
.Call("rewind_sysfile",ptr) |
| 107 |
} |
| 108 |
else |
| 109 |
ncases <- NA |
| 110 |
} |
| 111 |
attr(ptr,"ncases") <- ncases |
| 112 |
document <- data.spec$document |
| 113 |
data.spec$document <- NULL |
| 114 |
|
| 115 |
aux <- data.spec$auxiliaries |
| 116 |
if(length(aux$aux_var) && !ignore.scale.info){ |
| 117 |
aux_var <- do.call("rbind",aux$aux_var) |
| 118 |
mlev <- aux_var[,1] |
| 119 |
mlev[mlev==0] <- 1 |
| 120 |
mlev <- c("nominal","ordinal","interval")[mlev] |
| 121 |
variables <- mapply("measurement<-",variables,mlev) |
| 122 |
} |
| 123 |
|
| 124 |
if(to.lower){ |
| 125 |
names(variables) <- tolower(names(variables)) |
| 126 |
} |
| 127 |
|
| 128 |
if(length(aux$aux_enc)){ |
| 129 |
encoding2 <- toupper(aux$aux_enc) |
| 130 |
if(encoding2 %in% iconvlist()){ |
| 131 |
encoding <- encoding2 |
| 132 |
message(sprintf("File character set is '%s'.",encoding)) |
| 133 |
} |
| 134 |
else { |
| 135 |
warning(sprintf("File contains unsupported character set '%s', using fallback.",encoding2)) |
| 136 |
encoding <- encoded |
| 137 |
} |
| 138 |
} |
| 139 |
else if(length(aux$info_int32)){ |
| 140 |
encoding <- aux$info_int32["character_code"] |
| 141 |
enc_table <- c("EBCDICUS"=1, |
| 142 |
"ASCII"=2, |
| 143 |
"ASCII"=3, |
| 144 |
"DEC Kanji"=4, |
| 145 |
"CP1250"=1250, |
| 146 |
"CP1252"=1252, |
| 147 |
"ISO8859-1"=28591, |
| 148 |
"UTF-8"=65001) |
| 149 |
encoding1 <- names(enc_table)[match(encoding,enc_table)] |
| 150 |
if(encoding1 %in% iconvlist()){ |
| 151 |
encoding <- encoding1 |
| 152 |
message(sprintf("File character set is '%s'.",encoding)) |
| 153 |
} |
| 154 |
else { |
| 155 |
warning(sprintf("File contains unsupported character set '%s', using fallback.",encoding1)) |
| 156 |
encoding <- encoded |
| 157 |
} |
| 158 |
} |
| 159 |
else { |
| 160 |
encoding <- encoded |
| 161 |
message("File does not contain character set encoding information, using fallback.") |
| 162 |
} |
| 163 |
|
| 164 |
if(iconv){ |
| 165 |
message(sprintf("Converting character set to UTF-8.")) |
| 166 |
variables <- lapply(variables,Iconv,from=encoding,to="UTF-8") |
| 167 |
} |
| 168 |
|
| 169 |
warn_if_duplicate_labels(variables) |
| 170 |
|
| 171 |
new("spss.system.importer", |
| 172 |
variables, |
| 173 |
ptr=ptr, |
| 174 |
varlab.file=varlab.file, |
| 175 |
codes.file=codes.file, |
| 176 |
missval.file=missval.file, |
| 177 |
document=document, |
| 178 |
data.spec=data.spec, |
| 179 |
encoded=encoding |
| 180 |
) |
| 181 |
} |
| 182 |
setMethod("initialize","spss.system.importer",function(.Object, |
| 183 |
variables, |
| 184 |
ptr, |
| 185 |
varlab.file=character(), |
| 186 |
codes.file=character(), |
| 187 |
missval.file=character(), |
| 188 |
document=character(), |
| 189 |
data.spec, |
| 190 |
encoded=encoded |
| 191 |
){ |
| 192 |
[email protected] <- variables |
| 193 |
.Object@ptr <- ptr |
| 194 |
[email protected] <- as.character(varlab.file) |
| 195 |
[email protected] <- as.character(codes.file) |
| 196 |
[email protected] <- as.character(missval.file) |
| 197 |
.Object@document <- as.character(document) |
| 198 |
[email protected] <- data.spec |
| 199 |
.Object@encoded <- encoded |
| 200 |
.Object |
| 201 |
}) |
| 202 |
|
| 203 |
|
| 204 |
setMethod("getNobs","spss.system.importer",function(x){ |
| 205 |
ncases <- attr(x@ptr,"ncases") |
| 206 |
if(!length(ncases)) { |
| 207 |
.Call("rewind_sysfile",x@ptr) |
| 208 |
attr(x@ptr,"ncases") <- ncases <- .Call("count_cases_sysfile",x@ptr) |
| 209 |
.Call("rewind_sysfile",x@ptr) |
| 210 |
} |
| 211 |
ncases |
| 212 |
}) |
| 213 |
|
| 214 |
setMethod("seekData","spss.system.importer",function(x){ |
| 215 |
if(!.Call("check_pointer",x@ptr)) stop("pointer is NULL, you need to recreate the object") |
| 216 |
.Call("rewind_sysfile",x@ptr) |
| 217 |
}) |
| 218 |
|
| 219 |
setMethod("readData","spss.system.importer", |
| 220 |
function(x,n){ |
| 221 |
if(!.Call("check_pointer",x@ptr)) stop("pointer is NULL, you need to recreate the object") |
| 222 |
iconv_list(.Call("read_sysfile_data", |
| 223 |
x@ptr, |
| 224 |
what=x, |
| 225 |
ncases=n, |
| 226 |
[email protected]$types), |
| 227 |
encoded=x@encoded) |
| 228 |
}) |
| 229 |
|
| 230 |
setMethod("readSlice","spss.system.importer", |
| 231 |
function(x,rows,cols){ |
| 232 |
if(!.Call("check_pointer",x@ptr)) stop("pointer is NULL, you need to recreate the object") |
| 233 |
iconv_list(.Call("read_sysfile_slice",x@ptr, |
| 234 |
what=x, |
| 235 |
j=cols,i=rows, |
| 236 |
[email protected]$types), |
| 237 |
encoded=x@encoded) |
| 238 |
}) |
| 239 |
|
| 240 |
setMethod("readChunk","spss.system.importer", |
| 241 |
function(x,nrows,cols){ |
| 242 |
if(!.Call("check_pointer",x@ptr)) stop("pointer is NULL, you need to recreate the object") |
| 243 |
iconv_list(.Call("read_sysfile_chunk",x@ptr, |
| 244 |
what=x, |
| 245 |
vars=cols,n=nrows, |
| 246 |
[email protected]$types), |
| 247 |
encoded=x@encoded) |
| 248 |
}) |
| 249 |
|
| 250 |
|
| 251 |
setMethod("show","spss.system.importer", |
| 252 |
function(object){ |
| 253 |
file.name <- attr(object@ptr,"file.name") |
| 254 |
nobs <- nrow(object) |
| 255 |
nvar <- ncol(object) |
| 256 |
varlab.file <- [email protected] |
| 257 |
codes.file <- [email protected] |
| 258 |
missval.file <- [email protected] |
| 259 |
cat("\nSPSS system file",sQuote(file.name),"\n\twith ") |
| 260 |
cat(nvar,"variables and ") |
| 261 |
cat(nobs,"observations\n") |
| 262 |
if(length(varlab.file)) cat("\twith variable labels from file",sQuote(varlab.file),"\n") |
| 263 |
if(length(codes.file)) cat("\twith value labels from file",sQuote(codes.file),"\n") |
| 264 |
if(length(missval.file)) cat("\twith missing value definitions from file",sQuote(missval.file),"\n") |
| 265 |
}) |
| 266 |
|
| 267 |
subset.spss.system.importer <- subset.importer |