From atp at piskorski.com  Sun Apr 27 17:31:25 2014
From: atp at piskorski.com (Andrew Piskorski)
Date: Sun, 27 Apr 2014 11:31:25 -0400
Subject: [Rd] Please make Pre-3.1 read.csv (type.convert) behavior
 available
In-Reply-To: <D56A602D-9044-400D-B12A-196A5EAEC05D@0xdata.com>
References: <D56A602D-9044-400D-B12A-196A5EAEC05D@0xdata.com>
Message-ID: <20140427153125.GA20280@piskorski.com>

On Fri, Apr 25, 2014 at 09:23:23PM -0700, Tom Kraljevic wrote:

> This, needless to say, is disruptive for us.  (Actually, it was downright shocking.)

It WAS somewhat shocking.  I trust the R core team to get things
right, and (AFAICT) they nearly always do.  This was an exception, and
shocking mostly in that it was so obviously wrong to completely
discard all possibility of backwards compatibility.

The old type.convert() functionality worked fine and was very useful,
so the *obviously* right thing to do would be to at least retain the
old behavior as a (non-default) option.

Reproducing the old behavior in user R code is not simple.  For
anybody else stuck with this, you can do it (probably inefficiently)
with the two functions below.  Create your own version of read.table()
that calls the dtk.type.convert() below instead of the stock
type.convert().  It's not pretty, but that will do it.


dtk.type.convert <- function(xx ,... ,ignore.signif.p=TRUE) { 
   # Add backwards compatibility to R 3.1's "new feature": 
   if(ignore.signif.p && all(dtk.can.be.numeric(xx ,ignore.na.p=TRUE))) { 
      if(all(is.na(xx))) type.convert(xx ,...) 
      else methods::as(xx ,"numeric")  
   } else type.convert(xx ,...) 
} 

dtk.can.be.numeric <- function(xx ,ignore.na.p=TRUE) { 
   # Test whether a value can be converted to numeric without becoming NA. 
   # AKA, can this value be usefully represented as numeric? 
   # Optionally ignore NAs already present in the incoming data. 

   old.warn <- options(warn = -1) ; on.exit(options(old.warn)) 
   aa <- !is.na(as.numeric(xx)) 
   if(ignore.na.p) (is.na(xx) | aa) else aa 
}

-- 
Andrew Piskorski <atp at piskorski.com>



[Rd] Please make Pre-3.1 read.csv (type.convert) behavior available
Andrew Piskorski