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