[R-gregmisc-users] SF.net SVN: r-gregmisc:[1926] trunk/gdata
Brought to you by:
warnes
From: <wa...@us...> - 2015-04-14 21:53:00
|
Revision: 1926 http://sourceforge.net/p/r-gregmisc/code/1926 Author: warnes Date: 2015-04-14 21:52:53 +0000 (Tue, 14 Apr 2015) Log Message: ----------- Add 'scientific' argument to write.fwf to allow control of whether numeric values can be displated using scientific notation. Modified Paths: -------------- trunk/gdata/R/write.fwf.R trunk/gdata/man/write.fwf.Rd Modified: trunk/gdata/R/write.fwf.R =================================================================== --- trunk/gdata/R/write.fwf.R 2015-04-14 20:52:26 UTC (rev 1925) +++ trunk/gdata/R/write.fwf.R 2015-04-14 21:52:53 UTC (rev 1926) @@ -20,6 +20,7 @@ width=NULL, eol="\n", qmethod=c("escape", "double"), + scientific=TRUE, ...) { ## --- Setup --- @@ -29,6 +30,14 @@ if(length(na) > 1) stop("only single value can be defined for 'na'") + if(!scientific) + { + option.scipen <- getOption("scipen") + on.exit( function() options("scipen"=option.scipen) ) + options("scipen"=100) + } + + if(rownames) { x <- cbind(rownames(x), x) rowColVal <- ifelse(!is.null(rowCol), rowCol, "row") Modified: trunk/gdata/man/write.fwf.Rd =================================================================== --- trunk/gdata/man/write.fwf.Rd 2015-04-14 20:52:26 UTC (rev 1925) +++ trunk/gdata/man/write.fwf.Rd 2015-04-14 21:52:53 UTC (rev 1926) @@ -23,7 +23,7 @@ write.fwf(x, file="", append=FALSE, quote=FALSE, sep=" ", na="", rownames=FALSE, colnames=TRUE, rowCol=NULL, justify="left", formatInfo=FALSE, quoteInfo=TRUE, width=NULL, eol="\n", - qmethod=c("escape", "double"), \dots) + qmethod=c("escape", "double"), scientific=TRUE, \dots) } @@ -54,6 +54,8 @@ '"escape"' (default), in which case the quote character is escaped in C style by a backslash, or '"double"', in which case it is doubled. You can specify just the initial letter.} + \item{scientific}{logical, if TRUE, allow numeric values to be + formatted using scientific notation.} \item{\dots}{further arguments to \code{\link{format.info}} and \code{\link{format}} } @@ -61,10 +63,10 @@ \details{ -*F*ixed *w*idth *f*ormat is not used widely anymore. Use some other -format (say *c*omma *s*eparated *v*alues; see \code{\link{read.csv}}) if -you can. However, if you need fixed width format then \code{write.fwf} -can help you. +While *F*ixed *w*idth *f*ormat is no longer widely used, it remains +common in some disciplines. When possible, we recommend using +standardized format (e.g. *c*omma *s*eparated *v*alues; see +\code{\link{read.csv}}) when possible. Output is similar to \code{print(x)} or \code{format(x)}. Formatting is done completely by \code{\link{format}} on a column basis. Columns in @@ -217,15 +219,22 @@ ## Force wider columns write.fwf(x=testData[, 1:5], width=20) + ## Show effect of 'scienfic' option + testData$num3 <- testData$num3 * 1e8 + write.fwf(testData, scientific=TRUE) + write.fwf(testData, scientific=FALSE) + testData$num3 <- testData$num3 / 1e8 + ## Write to file and report format and fixed width information file <- tempfile() formatInfo <- write.fwf(x=testData, file=file, formatInfo=TRUE) + formatInfo ## Read exported data back to R (note +1 due to separator) ## ... without header read.fwf(file=file, widths=formatInfo$width + 1, header=FALSE, skip=1, strip.white=TRUE) - + ## ... with header - via postimport modfication tmp <- read.fwf(file=file, widths=formatInfo$width + 1, skip=1, strip.white=TRUE) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |