[Rd] read.fwf; bug reports 8226 & 8236 (PR#8284)

rolf@math.unb.ca rolf at math.unb.ca
Sat Nov 5 16:56:47 CET 2005

It seems to me that the bug dealt with in bug reports 8226 and 8236
is still not fixed.

I obtained the revised version of read.fwf from the latest
R-patched and tried

	try.it <- read.fwf("junk",w,header=TRUE,as.is=TRUE)

This gave the error

Error in read.table(file = FILE, header = header, sep = sep, as.is = as.is,  : 
        more columns than column names

Inspection of file ``FILE'' revealed that (of course) the
first line of ``FILE'' was not tab separated and was hence
being treated as a single field.

I then tried the last of Emmanuel Paradis' proposed fixes; this
didn't quite work --- an extra ``sep'' (tab) gets tacked on at the
end of the line being catted and makes read.table think that there is
one more field than there actually are.

So I modified the fix to:

	if (header) {
        	headerline <- readLines(file, n = 1)
        	head.last  <- cumsum(widths)
        	head.first <- head.last - widths + 1
        	headerline <- substring(headerline,head.first,head.last)[!drop]
        	headerline <- paste(headerline,collapse=sep)
        	cat(file = FILE, headerline, "\n")

This works for me.

Brian Ripley says that this will crash with multiline records.  Well,
at least it works with single line records, as the current version
seems not to.

					Rolf Turner
					rolf at math.unb.ca

More information about the R-devel mailing list