[R-gregmisc-users] SF.net SVN: r-gregmisc: [1171] trunk/SASxport
Brought to you by:
warnes
From: <wa...@us...> - 2007-09-11 21:22:33
|
Revision: 1171 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1171&view=rev Author: warnes Date: 2007-09-11 14:22:31 -0700 (Tue, 11 Sep 2007) Log Message: ----------- Correct error in handling 'verbose' argument, error when more than one dataset has the same name, and add 'as.list' argument to ensure return value is a list, even if there is only one dataset in the file Modified Paths: -------------- trunk/SASxport/R/read.xport.R trunk/SASxport/man/read.xport.Rd Modified: trunk/SASxport/R/read.xport.R =================================================================== --- trunk/SASxport/R/read.xport.R 2007-09-11 21:21:09 UTC (rev 1170) +++ trunk/SASxport/R/read.xport.R 2007-09-11 21:22:31 UTC (rev 1171) @@ -12,7 +12,8 @@ keep=NULL, drop=NULL, as.is=0.95, # Prevent factor conversion if 95% or more unique - verbose=FALSE + verbose=FALSE, + as.list=FALSE ) { sasdateform <- @@ -23,9 +24,9 @@ if(verbose) { - oldOptionsDebug <- options("DEBUG") + oldOptions <- options("DEBUG") options(DEBUG=TRUE) - on.exit(options(DEBUG=oldOptionsDebug)) + on.exit(options(oldOptions)) } if(length(grep('http://', file))>0 || length(grep('ftp://', file))>0 ) @@ -55,6 +56,14 @@ scat("Reading the data file...") ds <- foreign:::read.xport(file) + if(any(duplicated(names(dsinfo)))) # only true if file contains has more than one data set + { + warning("Duplicate data set names in file. Data set names have been made unique.") + names(dsinfo) <- make.unique(names(dsinfo)) + names(ds) <- make.unique(names(ds)) + } + + if( (length(keep)>0 || length(drop)>0) ) ds <- ds[whichds] @@ -124,12 +133,10 @@ else names.tolower <- function(x) x - if(nds > 1) - { - res <- vector('list', nds) - names(res) <- gsub('_','.',dsn) - } + res <- vector('list', nds) + names(res) <- gsub('_','.',dsn) + possiblyConvertChar <- (is.logical(as.is) && !as.is) || (is.numeric(as.is) && as.is > 0) j <- 0 @@ -197,14 +204,14 @@ } lz <- lab[nam[i]] - if(lz != '') { + if(!is.null(lz) && length(lz)>0 && !is.na(lz) && lz != '') { names(lz) <- NULL label(x) <- lz changed <- TRUE } fmt <- fmt[nam[i]]; - if( !is.null(fmt) && !is.na(fmt) && fmt > '') { + if( !is.null(fmt) && length(fmt)>0 && !is.na(fmt) && fmt > '') { names(fmt) <- NULL formats(x) <- fmt changed <- TRUE @@ -216,14 +223,13 @@ scat('.') - if(nds>1) - res[[j]] <- w + res[[j]] <- w } scat("Done") - - if(nds > 1) + if(nds > 1 || as.list) res - else w + else + w } Modified: trunk/SASxport/man/read.xport.Rd =================================================================== --- trunk/SASxport/man/read.xport.Rd 2007-09-11 21:21:09 UTC (rev 1170) +++ trunk/SASxport/man/read.xport.Rd 2007-09-11 21:22:31 UTC (rev 1171) @@ -13,7 +13,8 @@ keep=NULL, drop=NULL, as.is=0.95, - verbose=FALSE + verbose=FALSE, + as.list=FALSE ) } \arguments{ @@ -60,12 +61,14 @@ } \item{verbose}{Logical indicating whether progress should be printed during the data loading and conversion process.} + \item{as.list}{Logical indicating whether to return a list even if + the SAS xport file contains only only one dataset.} } \value{ - If there is more than one dataset in the transport file other than the - \code{PROC FORMAT} file, the result is a list of data frames - containing all the non-\code{PROC FORMAT} datasets. Otherwise the - result a single data frame. + If there the trasport file only contains one dataset (not counting any + \code{PROC FORMAT} datasets) and \code{as.list=FALSE}, the result is a + single data set. Otherwise the result is a list + containing all the non-\code{PROC FORMAT} datasets. } \details{ @@ -156,6 +159,14 @@ url <- 'http://biostat.mc.vanderbilt.edu/cgi-bin/viewvc.cgi/*checkout*/Hmisc/trunk/tests/test2.xpt' w <- read.xport(url) + +# We can also get the dataset wrapped in a list +w <- read.xport(url, as.list=TRUE) + +\dontshow{ +assert( is.data.frame(w)==FALSE && is.list(w)==TRUE ) +} + \dontrun{ ## The Hmisc library provides many useful functions for interacting with ## data imported from SAS via read.xport() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |