Thread: [R-gregmisc-users] SF.net SVN: r-gregmisc: [1123] trunk/SASxport/R
Brought to you by:
warnes
From: <wa...@us...> - 2007-08-08 18:53:47
|
Revision: 1123 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1123&view=rev Author: warnes Date: 2007-08-08 11:53:42 -0700 (Wed, 08 Aug 2007) Log Message: ----------- Updates Added Paths: ----------- trunk/SASxport/R/AFirst.lib.s trunk/SASxport/R/formats.R trunk/SASxport/R/iformat.R trunk/SASxport/R/importConvertDateTime.R trunk/SASxport/R/label.R trunk/SASxport/R/lookup.xport.R trunk/SASxport/R/makeNames.R trunk/SASxport/R/read.xport.R trunk/SASxport/R/testDateTime.R trunk/SASxport/R/units.R Removed Paths: ------------- trunk/SASxport/R/xport.R Added: trunk/SASxport/R/AFirst.lib.s =================================================================== --- trunk/SASxport/R/AFirst.lib.s (rev 0) +++ trunk/SASxport/R/AFirst.lib.s 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,19 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + +## $Id: AFirst.lib.s,v 1.6 2005/09/26 15:44:17 dupontct Exp $ +under.unix <- !(version$os=='Microsoft Windows' || + version$os=='Win32' || version$os=='mingw32') + +.R. <- TRUE +.SV4. <- FALSE + +.noGenenerics <- TRUE # faster loading as new methods not used + +if(!exists('existsFunction')) { + existsFunction <- function(...) exists(..., mode='function') +} + Added: trunk/SASxport/R/formats.R =================================================================== --- trunk/SASxport/R/formats.R (rev 0) +++ trunk/SASxport/R/formats.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,20 @@ +formats <- function(x, default) + UseMethod("formats") + +formats.default <- function(x, default=NULL) +{ + lab <- attr(x,"format") + if(is.null(lab)) + default + else + lab +} + +"formats<-" <- function(x, value) + UseMethod("formats<-") + +"formats<-.default" <- function(x, value) +{ + attr(x,'format') <- value + x +} Added: trunk/SASxport/R/iformat.R =================================================================== --- trunk/SASxport/R/iformat.R (rev 0) +++ trunk/SASxport/R/iformat.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,20 @@ +iformat <- function(x, default) + UseMethod("iformat") + +iformat.default <- function(x, default=NULL) +{ + lab <- attr(x,"iformat") + if(is.null(lab)) + default + else + lab +} + +"iformat<-" <- function(x, value) + UseMethod("iformat<-") + +"iformat<-.default" <- function(x, value) +{ + attr(x,'iformat') <- value + x +} Added: trunk/SASxport/R/importConvertDateTime.R =================================================================== --- trunk/SASxport/R/importConvertDateTime.R (rev 0) +++ trunk/SASxport/R/importConvertDateTime.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,40 @@ +importConvertDateTime <- + function(x, type=c('date','time','datetime'), + input=c('sas','spss','dataload'), form) +{ + type <- match.arg(type) + input <- match.arg(input) + + if(input != 'sas' && type != 'date') + stop('only date variables are support for spss, dataload') + + if(.R.) { + adjdays <- c(sas=3653, spss=141428, dataload=135080)[input] + ## 1970-1-1 minus 1960-1-1, 1582-10-14, or 1600-3-1 + if(input=='spss') x <- x/86400 + + switch(type, + date = structure(x - adjdays, class='Date'), + time = { + ## Don MacQueen 3Apr02 + z <- structure(x, class=c('POSIXt','POSIXct')) + f <- format(z, tz='GMT') + z <- as.POSIXct(format(z, tz='GMT'), tz='') + structure(z, class=c('timePOSIXt','POSIXt','POSIXct')) + }, + datetime = { + chron((x - adjdays*86400)/86400, + out.format=c(dates='day mon year', times='h:m:s')) + } + ) + } else if(.SV4.) + switch(type, + date = timeDate(julian=x, format=form), + time = timeDate(ms=x*1000, format=form), + datetime = timeDate(julian=x/86400, format=form)) + else + switch(type, + date = dates(x, out.format=form), + time = chron(x/86400, out.format=form), + datetime = chron(x/86400, out.format=form)) +} Added: trunk/SASxport/R/label.R =================================================================== --- trunk/SASxport/R/label.R (rev 0) +++ trunk/SASxport/R/label.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,20 @@ +label <- function(x, default) + UseMethod("label") + +label.default <- function(x, default=NULL) +{ + lab <- attr(x,"label") + if(is.null(lab)) + default + else + lab +} + +"label<-" <- function(x, value) + UseMethod("label<-") + +"label<-.default" <- function(x, value) +{ + attr(x,'label') <- value + x +} Added: trunk/SASxport/R/lookup.xport.R =================================================================== --- trunk/SASxport/R/lookup.xport.R (rev 0) +++ trunk/SASxport/R/lookup.xport.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,2 @@ +## Simply make this accessible here as a convenience to the user +lookup.xport <- foreign:::lookup.xport Added: trunk/SASxport/R/makeNames.R =================================================================== --- trunk/SASxport/R/makeNames.R (rev 0) +++ trunk/SASxport/R/makeNames.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,12 @@ +makeNames <- function(names, unique=FALSE, allow=NULL) +{ + ## Runs make.names with exceptions in vector allow + ## By default, R 1.9 make.names is overridden to convert _ to . as + ## with S-Plus and previous versions of R. Specify allow='_' otherwise. + if(!.R. & length(allow)) + stop('does not apply for S-Plus') + n <- make.names(names, unique) + if(!length(allow)) + n <- gsub('_', '.', n) + n +} Added: trunk/SASxport/R/read.xport.R =================================================================== --- trunk/SASxport/R/read.xport.R (rev 0) +++ trunk/SASxport/R/read.xport.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,214 @@ +read.xport <- function(file, + force.integer=TRUE, + formats=NULL, + name.chars=NULL, + names.tolower=TRUE, + keep=NULL, + drop=NULL, + as.is=0.95, # Prevent factor conversion if 95% or more unique + verbose=FALSE + ) + { + sasdateform <- + toupper(c("date","mmddyy","yymmdd","ddmmyy","yyq","monyy", + "julian","qtr","weekdate","weekdatx","weekday","month")) + sastimeform <- toupper(c("hhmm","hour","mmss","time")) + sasdatetimeform <- toupper(c("datetime","tod")) + + if(length(grep('http://', file))) { + tf <- tempfile() + download.file(file, tf, mode='wb', quiet=TRUE) + file <- tf + } + + if(verbose) + { + oldOptionsDebug <- options("DEBUG") + options(DEBUG=TRUE) + on.exit(options(DEBUG=oldOptionsDebug)) + } + + scat("Extracting data file information...") + dsinfo <- foreign:::lookup.xport(file) + + if(length(keep)) + whichds <- toupper(keep) + else + + whichds <- setdiff(names(dsinfo), c(toupper(drop),'_CONTENTS_','_contents_')) + + scat("Reading the data file...") + ds <- foreign:::read.xport(file) + + if( (length(keep)>0 || length(drop)>0) ) + ds <- ds[whichds] + + scat("Processing contents...") + ## PROC FORMAT CNTLOUT= dataset present? + fds <- NULL + if(!length(formats)) { + fds <- sapply(dsinfo, function(x) + all(c('FMTNAME','START','END','MIN','MAX','FUZZ') + %in% x$name)) + fds <- names(fds)[fds] + if(length(fds) > 1) { + warning('transport file contains more than one PROC FORMAT CNTLOUT= dataset; using only the first') + fds <- fds[1] + } + } + + finfo <- NULL + if(length(formats) || length(fds)) { + if(length(formats)) + finfo <- formats + else + finfo <- ds[[fds]] + + ## Remove leading $ from char format names + ## fmtname <- sub('^\\$','',as.character(finfo$FMTNAME)) + fmtname <- as.character(finfo$FMTNAME) + finfo <- split(finfo[c('START','END','LABEL')], fmtname) + finfo <- lapply(finfo, + function(f) + { + rb <- function(a) + { # remove leading + trailing blanks + a <- sub('[[:space:]]+$', '', as.character(a)) + sub('^[[:space:]]+', '', a) + } + + st <- rb(f$START) + en <- rb(f$END) + lab <- rb(f$LABEL) + ##j <- is.na(st) | is.na(en) + ## st %in% c('','.','NA') | en %in% c('','.','NA') + j <- is.na(st) | is.na(en) | st == '' | en == '' + if(any(j)) { + warning('NA in code in FORMAT definition; removed') + st <- st[!j]; en <- en[!j]; lab <- lab[!j] + } + + if(!all(st==en)) + return(NULL) + + list(value = all.is.numeric(st, 'vector'), + label = lab) + }) + } + + ## Number of non-format datasets + nods <- length(whichds) + nds <- nods - (length(formats) == 0 && length(finfo) > 0) + which.regular <- setdiff(whichds, fds) + dsn <- tolower(which.regular) + + + ## Handle lowercase name conversions + if(names.tolower) + names.tolower <- tolower + else + names.tolower <- function(x) x + + if(nds > 1) + { + res <- vector('list', nds) + names(res) <- gsub('_','.',dsn) + } + + possiblyConvertChar <- (is.logical(as.is) && !as.is) || + (is.numeric(as.is) && as.is > 0) + j <- 0 + for(k in which.regular) { + j <- j + 1 + scat('Processing SAS dataset', k) + w <- + if(nods==1) + ds + else ds[[k]] + + scat('.') + + if(!length(w)) { + scat('Empty dataset', k, 'ignored\n') + next + } + + nam <- names.tolower(makeNames(names(w), allow=name.chars)) + names(w) <- nam + dinfo <- dsinfo[[k]] + fmt <- sub('^\\$','',dinfo$format) + lab <- dinfo$label + ndinfo <- names.tolower(makeNames(dinfo$name, allow=name.chars)) + names(lab) <- names(fmt) <- ndinfo + for(i in 1:length(w)) { + changed <- FALSE + x <- w[[i]] + fi <- fmt[nam[i]]; names(fi) <- NULL + if(fi != '' && length(finfo) && (fi %in% names(finfo))) { + f <- finfo[[fi]] + if(length(f)) { ## may be NULL because had a range in format + x <- factor(x, f$value, f$label) + attr(x, 'format') <- fi + changed <- TRUE + } + } + + if(is.numeric(x)) { + if(fi %in% sasdateform) { + x <- importConvertDateTime(x, 'date', 'sas') + changed <- TRUE + } else if(fi %in% sastimeform) { + x <- importConvertDateTime(x, 'time', 'sas') + changed <- TRUE + } else if(fi %in% sasdatetimeform) { + x <- importConvertDateTime(x, 'datetime', 'sas') + changed <- TRUE + } else if(force.integer) { + if(all(is.na(x))) { + storage.mode(x) <- 'integer' + changed <- TRUE + } else if(max(abs(x),na.rm=TRUE) <= (2^31-1) && + all(floor(x) == x, na.rm=TRUE)) { + storage.mode(x) <- 'integer' + changed <- TRUE + } + } + } else if(possiblyConvertChar && is.character(x)) { + if((is.logical(as.is) && !as.is) || + (is.numeric(as.is) && length(unique(x)) < as.is*length(x))) { + x <- factor(x, exclude='') + changed <- TRUE + } + } + + lz <- lab[nam[i]] + if(lz != '') { + names(lz) <- NULL + label(x) <- lz + changed <- TRUE + } + + fmt <- fmt[nam[i]]; + if( !is.null(fmt) && !is.na(fmt) && fmt > '') { + names(fmt) <- NULL + formats(x) <- fmt + changed <- TRUE + } + + if(changed) + w[[i]] <- x + } + + scat('.') + + if(nds>1) + res[[j]] <- w + } + + scat("Done") + + + if(nds > 1) + res + else w + } Added: trunk/SASxport/R/testDateTime.R =================================================================== --- trunk/SASxport/R/testDateTime.R (rev 0) +++ trunk/SASxport/R/testDateTime.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,36 @@ + +## Determine if variable is a date, time, or date/time variable in R +## or S-Plus. The following 2 functions are used by describe.vector +## timeUsed assumes is date/time combination variable and has no NAs +testDateTime <- function(x, what=c('either','both','timeVaries')) +{ + what <- match.arg(what) + cl <- class(x) # was oldClass 22jun03 + if(!length(cl)) + return(FALSE) + + dc <- if(.R.) + c('Date', 'POSIXt','POSIXct','dates','times','chron') + else + c('timeDate','date','dates','times','chron') + + dtc <- if(.R.) + c('POSIXt','POSIXct','chron') + else + c('timeDate','chron') + + switch(what, + either = any(cl %in% dc), + both = any(cl %in% dtc), + timeVaries = { + if('chron' %in% cl || 'Date' %in% cl || !.R.) { + ## chron or S+ timeDate + y <- as.numeric(x) + length(unique(round(y - floor(y),13))) > 1 + } + else if(.R.) + length(unique(format(x,'%H%M%S'))) > 1 + else + FALSE + }) +} Added: trunk/SASxport/R/units.R =================================================================== --- trunk/SASxport/R/units.R (rev 0) +++ trunk/SASxport/R/units.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -0,0 +1,20 @@ +units <- function(x, default) + UseMethod("units") + +units.default <- function(x, default=NULL) +{ + lab <- attr(x,"units") + if(is.null(lab)) + default + else + lab +} + +"units<-" <- function(x, value) + UseMethod("units<-") + +"units<-.default" <- function(x, value) +{ + attr(x,'units') <- value + x +} Deleted: trunk/SASxport/R/xport.R =================================================================== --- trunk/SASxport/R/xport.R 2007-08-08 18:53:04 UTC (rev 1122) +++ trunk/SASxport/R/xport.R 2007-08-08 18:53:42 UTC (rev 1123) @@ -1,31 +0,0 @@ -### -### Read SAS xport format libraries -### -### Copyright 1999-1999 Douglas M. Bates <bates$stat.wisc.edu>, -### Saikat DebRoy <saikat$stat.wisc.edu> -### -### This file is part of the `foreign' library for R and related languages. -### It is made available under the terms of the GNU General Public -### License, version 2, or at your option, any later version, -### incorporated herein by reference. -### -### This program is distributed in the hope that it will be -### useful, but WITHOUT ANY WARRANTY; without even the implied -### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR -### PURPOSE. See the GNU General Public License for more -### details. -### -### You should have received a copy of the GNU General Public -### License along with this program; if not, write to the Free -### Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, -### MA 02110-1301, USA - -lookup.xport <- function(file) .Call(xport_info, file) - - -read.xport <- function(file) { - data.info <- lookup.xport(file) - ans <- .Call(xport_read, file, data.info) - if (length(ans) == 1) as.data.frame(ans[[1]]) - else lapply(ans, as.data.frame) -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-08-09 16:54:19
|
Revision: 1128 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1128&view=rev Author: warnes Date: 2007-08-09 09:54:16 -0700 (Thu, 09 Aug 2007) Log Message: ----------- Add comment header indicating the source of code from Hmisc Modified Paths: -------------- trunk/SASxport/R/importConvertDateTime.R trunk/SASxport/R/makeNames.R trunk/SASxport/R/read.xport.R trunk/SASxport/R/testDateTime.R Modified: trunk/SASxport/R/importConvertDateTime.R =================================================================== --- trunk/SASxport/R/importConvertDateTime.R 2007-08-09 16:53:14 UTC (rev 1127) +++ trunk/SASxport/R/importConvertDateTime.R 2007-08-09 16:54:16 UTC (rev 1128) @@ -1,3 +1,9 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + importConvertDateTime <- function(x, type=c('date','time','datetime'), input=c('sas','spss','dataload'), form) Modified: trunk/SASxport/R/makeNames.R =================================================================== --- trunk/SASxport/R/makeNames.R 2007-08-09 16:53:14 UTC (rev 1127) +++ trunk/SASxport/R/makeNames.R 2007-08-09 16:54:16 UTC (rev 1128) @@ -1,3 +1,9 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + makeNames <- function(names, unique=FALSE, allow=NULL) { ## Runs make.names with exceptions in vector allow Modified: trunk/SASxport/R/read.xport.R =================================================================== --- trunk/SASxport/R/read.xport.R 2007-08-09 16:53:14 UTC (rev 1127) +++ trunk/SASxport/R/read.xport.R 2007-08-09 16:54:16 UTC (rev 1128) @@ -1,3 +1,9 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + read.xport <- function(file, force.integer=TRUE, formats=NULL, Modified: trunk/SASxport/R/testDateTime.R =================================================================== --- trunk/SASxport/R/testDateTime.R 2007-08-09 16:53:14 UTC (rev 1127) +++ trunk/SASxport/R/testDateTime.R 2007-08-09 16:54:16 UTC (rev 1128) @@ -1,3 +1,8 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## ## Determine if variable is a date, time, or date/time variable in R ## or S-Plus. The following 2 functions are used by describe.vector This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-08-09 23:28:30
|
Revision: 1131 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1131&view=rev Author: warnes Date: 2007-08-09 16:28:27 -0700 (Thu, 09 Aug 2007) Log Message: ----------- More changes, esp to lookup.xport() and friends Modified Paths: -------------- trunk/SASxport/R/AFirst.lib.s trunk/SASxport/R/lookup.xport.R trunk/SASxport/R/read.xport.R trunk/SASxport/R/write.xport.R Added Paths: ----------- trunk/SASxport/R/all.is.numeric.R trunk/SASxport/R/in.opererator.R Modified: trunk/SASxport/R/AFirst.lib.s =================================================================== --- trunk/SASxport/R/AFirst.lib.s 2007-08-09 19:02:22 UTC (rev 1130) +++ trunk/SASxport/R/AFirst.lib.s 2007-08-09 23:28:27 UTC (rev 1131) @@ -17,3 +17,9 @@ existsFunction <- function(...) exists(..., mode='function') } +if(.R.) { + ## create some function definitions just to avoid R CMD CHECK warnings + timeDate <- function(...) stop("Not Implemented") + dates <- function(...) stop("Not Implemented") +} + Added: trunk/SASxport/R/all.is.numeric.R =================================================================== --- trunk/SASxport/R/all.is.numeric.R (rev 0) +++ trunk/SASxport/R/all.is.numeric.R 2007-08-09 23:28:27 UTC (rev 1131) @@ -0,0 +1,22 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + +all.is.numeric <- function(x, what=c('test','vector'), extras=c('.','NA')) +{ + what <- match.arg(what) + old <- options(warn=-1) + on.exit(options(old)) + ##.Options$warn <- -1 6Aug00 + x <- sub('[[:space:]]+$', '', x) + x <- sub('^[[:space:]]+', '', x) + xs <- x[x %nin% c('',extras)] + isnum <- !any(is.na(as.numeric(xs))) + if(what=='test') + isnum + else if(isnum) + as.numeric(x) + else x +} Added: trunk/SASxport/R/in.opererator.R =================================================================== --- trunk/SASxport/R/in.opererator.R (rev 0) +++ trunk/SASxport/R/in.opererator.R 2007-08-09 23:28:27 UTC (rev 1131) @@ -0,0 +1,7 @@ +## +## Code originally from Frank Harrell's 'Hmisc' library: +## http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/Hmisc +## Copied with permission on 2007-08-04 +## + +"%nin%" <- function(a, b) ! (a %in% b) Modified: trunk/SASxport/R/lookup.xport.R =================================================================== --- trunk/SASxport/R/lookup.xport.R 2007-08-09 19:02:22 UTC (rev 1130) +++ trunk/SASxport/R/lookup.xport.R 2007-08-09 23:28:27 UTC (rev 1131) @@ -1,2 +1,62 @@ ## Simply make this accessible here as a convenience to the user -lookup.xport <- foreign:::lookup.xport +lookup.xport <- function(file) + { + fname <- file + + if(length(grep('http://', file))>0 || length(grep('ftp://', file))>0 ) + { + scat("Downloading file...") + tf <- tempfile() + download.file(file, tf, mode='wb', quiet=TRUE) + file <- tf + } + + ret <- foreign:::lookup.xport(file) + attr(ret, "call") <- match.call() + attr(ret, "file") <- fname + class(ret) <- c("lookup.xport", "list") + ret + } + +print.lookup.xport <- function(x, ...) + { + Sinfo <- summary(x, ...) + print(Sinfo) + } + + +summary.lookup.xport <- function(object, ...) + { + subFun <- function(XX) + { + df <- object[[XX]] + ret <- as.data.frame(df[c("name","type","format","width","label")]) + cbind(dataset=XX, ret, nobs=df$length) + } + + dFrames <- lapply( names(object), subFun ) + singleFrame <- do.call("rbind", dFrames) + rownames(singleFrame) <- paste(singleFrame$dataset, singleFrame$name, sep=".") + + attr(singleFrame, "call") <- attr(object, "call") + attr(singleFrame, "file") <- attr(object, "file") + class(singleFrame) <- c("summary.lookup.xport","data.frame") + + singleFrame + } + +print.summary.lookup.xport <- function(x, ...) +{ + cat("\n") + cat("SAS xport file\n") + cat("--------------\n"); + cat("Filename: `", attr(x,"file"), "'\n", sep="") + cat("\n") + for(dSetName in levels(x$dataset)) + { + cat("Variables in data set `", dSetName, "':\n", sep="") + print(as.data.frame(x)[x$dataset==dSetName,], row.names=FALSE) + cat("\n") + } +} + Modified: trunk/SASxport/R/read.xport.R =================================================================== --- trunk/SASxport/R/read.xport.R 2007-08-09 19:02:22 UTC (rev 1130) +++ trunk/SASxport/R/read.xport.R 2007-08-09 23:28:27 UTC (rev 1131) @@ -21,12 +21,6 @@ sastimeform <- toupper(c("hhmm","hour","mmss","time")) sasdatetimeform <- toupper(c("datetime","tod")) - if(length(grep('http://', file))) { - tf <- tempfile() - download.file(file, tf, mode='wb', quiet=TRUE) - file <- tf - } - if(verbose) { oldOptionsDebug <- options("DEBUG") @@ -34,6 +28,14 @@ on.exit(options(DEBUG=oldOptionsDebug)) } + if(length(grep('http://', file))>0 || length(grep('ftp://', file))>0 ) + { + scat("Downloading file...") + tf <- tempfile() + download.file(file, tf, mode='wb', quiet=TRUE) + file <- tf + } + scat("Extracting data file information...") dsinfo <- foreign:::lookup.xport(file) Modified: trunk/SASxport/R/write.xport.R =================================================================== --- trunk/SASxport/R/write.xport.R 2007-08-09 19:02:22 UTC (rev 1130) +++ trunk/SASxport/R/write.xport.R 2007-08-09 23:28:27 UTC (rev 1131) @@ -51,10 +51,10 @@ scat("Done") if(file==stdout()) - out <- function(what) + out <- function(...) { - cat("ASCII: ", rawToDisplay(what), "") - cat("HEX: ", what, "") + cat("ASCII: ", rawToDisplay(...), "") + cat("HEX: ", ..., "") } else out <- function(...) writeBin( ..., raw(), con=file) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <wa...@us...> - 2007-11-01 06:14:30
|
Revision: 1203 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1203&view=rev Author: warnes Date: 2007-10-31 23:14:22 -0700 (Wed, 31 Oct 2007) Log Message: ----------- - Use of the attribute named 'format' caused problems with chron objects. Consequently, the format information is now stored in the 'SASformat' attribute. For consistency, the input format information is now stored in the 'SASiformat' attribute. - The functions for extracting and setting the format and iformat information have been renamed to 'SASformat' and 'SASiformat', etc. - In order to properly handle SAS format information, we now use a locally modified version of foreign::lookup.xport and foreign::read.xport. - Various typo corrections - Creation of a new function fstr() to generate sas format name strings using name, length, and digits information. - Addion of a toSAS method for chron() objects Modified Paths: -------------- trunk/SASxport/R/lookup.xport.R trunk/SASxport/R/make.formats.R trunk/SASxport/R/read.xport.R trunk/SASxport/R/toSAS.R trunk/SASxport/R/write.xport.R Added Paths: ----------- trunk/SASxport/R/SASformat.R trunk/SASxport/R/SASiformat.R trunk/SASxport/R/fstr.R Removed Paths: ------------- trunk/SASxport/R/formats.R trunk/SASxport/R/iformat.R Copied: trunk/SASxport/R/SASformat.R (from rev 1196, trunk/SASxport/R/formats.R) =================================================================== --- trunk/SASxport/R/SASformat.R (rev 0) +++ trunk/SASxport/R/SASformat.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -0,0 +1,36 @@ +SASformat <- function(x, default) + UseMethod("SASformat") + +SASformat.default <- function(x, default=NULL) +{ + lab <- attr(x,"SASformat") + if(is.null(lab)) + default + else + lab +} + +SASformat.data.frame <- function(x, default=NULL) +{ + sapply( x, SASformat) +} + +"SASformat<-" <- function(x, value) + UseMethod("SASformat<-") + +"SASformat<-.default" <- function(x, value) +{ + attr(x,'SASformat') <- value + x +} + + +"SASformat<-.data.frame" <- function(x, value) +{ + if( ncol(x) != length(value) ) + stop("vector of formats must match number of data frame columns") + + for(i in 1:ncol(x)) + attr(x[[i]],'SASformat') <- value[i] + x +} Copied: trunk/SASxport/R/SASiformat.R (from rev 1196, trunk/SASxport/R/iformat.R) =================================================================== --- trunk/SASxport/R/SASiformat.R (rev 0) +++ trunk/SASxport/R/SASiformat.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -0,0 +1,20 @@ +SASiformat <- function(x, default) + UseMethod("SASiformat") + +SASiformat.default <- function(x, default=NULL) +{ + lab <- attr(x,"SASiformat") + if(is.null(lab)) + default + else + lab +} + +"SASiformat<-" <- function(x, value) + UseMethod("SASiformat<-") + +"SASiformat<-.default" <- function(x, value) +{ + attr(x,'SASiformat') <- value + x +} Deleted: trunk/SASxport/R/formats.R =================================================================== --- trunk/SASxport/R/formats.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/formats.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -1,20 +0,0 @@ -formats <- function(x, default) - UseMethod("formats") - -formats.default <- function(x, default=NULL) -{ - lab <- attr(x,"format") - if(is.null(lab)) - default - else - lab -} - -"formats<-" <- function(x, value) - UseMethod("formats<-") - -"formats<-.default" <- function(x, value) -{ - attr(x,'format') <- value - x -} Added: trunk/SASxport/R/fstr.R =================================================================== --- trunk/SASxport/R/fstr.R (rev 0) +++ trunk/SASxport/R/fstr.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -0,0 +1,17 @@ +fstr <- function(name, length, digits) + { + invalid <- function(x) is.null(x) | ( length(x)<1 ) | ( nchar(x) < 1 ) | x==0 + inner <- function(i) + { + if( invalid(name[i]) ) + return("") + if( invalid( length[i] ) ) + return(name[i]) + if( invalid(digits[i]) ) + return( paste(name[i], length[i], '.', sep='' ) ) + else + return( paste(name[i], length[i], '.', digits[i], sep='' ) ) + } + sapply( 1:length(name), inner) + + } Deleted: trunk/SASxport/R/iformat.R =================================================================== --- trunk/SASxport/R/iformat.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/iformat.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -1,20 +0,0 @@ -iformat <- function(x, default) - UseMethod("iformat") - -iformat.default <- function(x, default=NULL) -{ - lab <- attr(x,"iformat") - if(is.null(lab)) - default - else - lab -} - -"iformat<-" <- function(x, value) - UseMethod("iformat<-") - -"iformat<-.default" <- function(x, value) -{ - attr(x,'iformat') <- value - x -} Modified: trunk/SASxport/R/lookup.xport.R =================================================================== --- trunk/SASxport/R/lookup.xport.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/lookup.xport.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -11,7 +11,7 @@ file <- tf } - ret <- foreign:::lookup.xport(file) + ret <- lookup.xport.inner(file) attr(ret, "call") <- match.call() attr(ret, "file") <- fname class(ret) <- c("lookup.xport", "list") @@ -30,7 +30,13 @@ subFun <- function(XX) { df <- object[[XX]] - ret <- as.data.frame(df[c("name","type","format","width","label")]) + ret <- as.data.frame(df[c( + "name", "type", + "format", "flength", "fdigits", + "iformat", "iflength", "ifdigits", + "label" + ) + ]) cbind(dataset=XX, ret, nobs=df$length) } Modified: trunk/SASxport/R/make.formats.R =================================================================== --- trunk/SASxport/R/make.formats.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/make.formats.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -1,9 +1,9 @@ ## Take a list of data frames and process factor objects: ## ## For each factor object -## 1) generate SAS format infromation +## 1) generate SAS format information ## 2) add the "factor" attribute to these factors with the name of the generated SAS format -## 3) add this SAS format information to the FORMATS datafram, creating it if necessary. +## 3) add this SAS format information to the FORMATS dataframe, creating it if necessary. ## ## Then return a new list of dataframe containing ## 1) The (potentially modified) data frames @@ -34,7 +34,7 @@ for(varName in colnames(df)) { var <- df[[varName]] - if(is.factor(var) && is.null(formats(var)) ) + if(is.factor(var) && is.null(SASformat(var)) ) { # We need unique format names, but SAS restricts # format names alpha characters. To create a unique @@ -50,7 +50,7 @@ make.format.factor(var, formatName ) ) - formats(var) <- formatName + SASformat(var) <- formatName df[[varName]] <- var } } Modified: trunk/SASxport/R/read.xport.R =================================================================== --- trunk/SASxport/R/read.xport.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/read.xport.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -46,16 +46,15 @@ stop("The specified file does not start with a SAS xport file header!") scat("Extracting data file information...") - dsinfo <- foreign:::lookup.xport(file) + dsinfo <- lookup.xport.inner(file) if(length(keep)) whichds <- toupper(keep) else - whichds <- setdiff(names(dsinfo), c(toupper(drop),'_CONTENTS_','_contents_')) scat("Reading the data file...") - ds <- foreign:::read.xport(file) + ds <- read.xport.inner(file) if(any(duplicated(names(dsinfo)))) # only true if file contains has more than one data set { @@ -128,10 +127,17 @@ nam <- names.tolower(makeNames(names(w), allow=name.chars)) names(w) <- nam dinfo <- dsinfo[[k]] + fmt <- sub('^\\$','',dinfo$format) + formats <- fstr( fmt, dinfo$flength, dinfo$fdigits) + + ifmt <- sub('^\\$','',dinfo$iformat) + iformats <- fstr( ifmt, dinfo$iflength, dinfo$ifdigits) + lab <- dinfo$label + ndinfo <- names.tolower(makeNames(dinfo$name, allow=name.chars)) - names(lab) <- names(fmt) <- ndinfo + names(lab) <- names(fmt) <- names(formats) <- names(iformats) <- ndinfo for(i in 1:length(w)) { changed <- FALSE x <- w[[i]] @@ -141,7 +147,7 @@ f <- finfo[[fi]] if(length(f)) { ## may be NULL because had a range in format x <- factor(x, f$value, f$label) - attr(x, 'format') <- fi + attr(x, 'SASformat') <- fi changed <- TRUE } } @@ -181,13 +187,18 @@ changed <- TRUE } - fmt <- fmt[nam[i]]; - if( !is.null(fmt) && length(fmt)>0 && !is.na(fmt) && fmt > '') { - names(fmt) <- NULL - formats(x) <- fmt - changed <- TRUE - } - + if( formats[nam[i]] > "" ) + { + SASformat(x) <- formats[[nam[i]]] + changed <- TRUE + } + + if( iformats[nam[i]] > "" ) + { + SASformat(x) <- formats[[nam[i]]] + changed <- TRUE + } + if(changed) w[[i]] <- x } @@ -199,10 +210,13 @@ scat("Done") - if(include.formats) + if( include.formats ) { nds <- nds+1 - res$"FORMATS" <- ds[[fds]] + if( length(fds)>0 ) + res$"FORMATS" <- ds[[fds]] + else + res$FORMATS <- empty.format.table() } Modified: trunk/SASxport/R/toSAS.R =================================================================== --- trunk/SASxport/R/toSAS.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/toSAS.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -1,42 +1,42 @@ toSAS <- function(x, format, format.info=NULL) UseMethod("toSAS") -toSAS.numeric <- function(x, format=formats(x), format.info=NULL) +toSAS.numeric <- function(x, format=SASformat(x), format.info=NULL) { retval <- as.numeric(x) - attr(retval, "format")=format + attr(retval, "SASformat")=format retval } -toSAS.logical <- function(x, format=formats(x), format.info=NULL) +toSAS.logical <- function(x, format=SASformat(x), format.info=NULL) { retval <- as.character(x) - attr(retval, "format")=format + attr(retval, "SASformat")=format retval } -toSAS.character <- function(x, format=formats(x), format.info=NULL) +toSAS.character <- function(x, format=SASformat(x), format.info=NULL) { retval <- as.character(x) - attr(retval, "format")=format + attr(retval, "SASformat")=format retval } -toSAS.factor <- function(x, format=formats(x), format.info=NULL) +toSAS.factor <- function(x, format=SASformat(x), format.info=NULL) { finfo <- process.formats(format.info) if( (length(format>0)) && (format %in% names(finfo)) ) { - labels <- finfo[[formats(x)]]$label - values <- finfo[[formats(x)]]$value + labels <- finfo[[SASformat(x)]]$label + values <- finfo[[SASformat(x)]]$value retval <- values[match( x, labels)] } else { retval <- as.character(x) } - attr(retval, "format")=format + attr(retval, "SASformat")=format retval } @@ -44,7 +44,7 @@ { sasBaseSeconds <- as.numeric(ISOdatetime(1960,1,1,0,0,0)) retval <- unclass(as.POSIXct(x)) - sasBaseSeconds # sasBaseSeconds is negative - attr(retval,"format") <- format + attr(retval,"SASformat") <- format retval } @@ -52,14 +52,15 @@ { sasBase <- as.Date(strptime("01/01/1960", "%m/%d/%Y", tz="GMT")) # days retval <- as.numeric( as.Date(x) - sasBase) - attr(retval, "format") <- format + attr(retval, "SASformat") <- format retval } -toSAS.default <- function(x, format=formats(x), format.info=NULL) +toSAS.default <- function(x, format=SASformat(x), format.info=NULL) { retval <- as.character(x) - attr(retval, "format")=format + attr(retval, "SASformat") <- format retval } +toSAS.chron <- toSAS.POSIXt Modified: trunk/SASxport/R/write.xport.R =================================================================== --- trunk/SASxport/R/write.xport.R 2007-11-01 06:01:50 UTC (rev 1202) +++ trunk/SASxport/R/write.xport.R 2007-11-01 06:14:22 UTC (rev 1203) @@ -204,8 +204,8 @@ # get attribute information before any transformations!" varLabel <- attr(var, "label") - varFormat <- attr(var, "format") - varIFormat <- attr(var, "iformat") + varFormat <- attr(var, "SASformat") + varIFormat <- attr(var, "SASiformat") # Convert R object to SAS object df[[i]] <- var <- toSAS(var, format.info=formats) @@ -233,15 +233,15 @@ out( xport.namestr( var=var, - varName=i, - varNum=varIndex, - varPos=lenIndex, - varLength=varLen, - varLabel=varLabel, - fName = formatInfo$name, + varName = i, + varNum = varIndex, + varPos = lenIndex, + varLength = varLen, + varLabel = varLabel, + fName = formatInfo$name, fLength = formatInfo$len, fDigits = formatInfo$digits, - iName = iFormatInfo$name, + iName = iFormatInfo$name, iLength = iFormatInfo$len, iDigits = iFormatInfo$digits, ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |