From rolf at math.unb.ca  Sat Nov  5 16:56:47 2005
From: rolf at math.unb.ca (rolf@math.unb.ca)
Date: Sat,  5 Nov 2005 16:56:47 +0100 (CET)
Subject: [Rd] read.fwf; bug reports 8226 & 8236 (PR#8284)
Message-ID: <20051105155647.9CCD123D1C@slim.kubism.ku.dk>

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.
				cheers,

					Rolf Turner
					rolf at math.unb.ca



[Rd] read.fwf; bug reports 8226 & 8236 (PR#8284)
rolf@math.unb.ca