title: append multiple large data.table's; custom data coercion using colClasses and fread; named pipes
body: <p>[This is kind of multiple bug-reports/feature requests in one post, but they don't necessarily make sense in isolation. Apologies for the monster post in advance. Posting here as suggested by help(data.table). Also, I'm new to R; so apologies if I'm not following best practices in my code below. I'm trying.]</p>
  
  <h2>1. <code>rbindlist</code> crash on 6 * 8GB files (I have 128GB RAM)</h2>
  
  <p>First I want to report that using rbindlist to append large data.tables causes R to segfault (ubuntu 13.10, the packaged R version 3.0.1-3ubuntu1, data.table installed from within R from CRAN). The machine has 128 GiB of RAM; so, I shouldn't be running out of memory given the size of the data.</p>
  
  <p>My code:</p>
  
  <pre><code>append.tables &lt;- function(files) {
      moves.by.year &lt;- lapply(files, fread)
      move &lt;- rbindlist(moves.by.year)
      rm(moves.by.year)
      move[,week_end := as.Date(as.character(week_end), format="%Y%m%d")]
      return(move)
  }
  </code></pre>
  
  <p>Crash message:</p>
  
  <pre><code> append.tables crashes with this:
  &gt; system.time(move &lt;- append.tables(files))
   *** caught segfault ***
  address 0x7f8e88dc1d10, cause 'memory not mapped'
  
  Traceback:
   1: rbindlist(moves.by.year)
   2: append.tables(files)
   3: system.time(move &lt;- append.tables(files))
  </code></pre>
  
  <p>There are 6 files, each about 8 GiB or 100 million lines long with 8 variables, tab separated.</p>
  
  <h2>2. Could <code>fread</code> accept multiple file names?</h2>
  
  <p>In any case, I think a better approach here would be to allow fread to take files as a vector of file names:</p>
  
  <pre><code>files &lt;- c("my", "files", "to be", "appended")
  dt &lt;- fread(files)
  </code></pre>
  
  <p>Presumably you can be much more memory efficient under the hood than without having to keep all of these objects around at the same time as appears to be necessary as a user of R.</p>
  
  <h2>3. <code>colClasses</code> gives an error message</h2>
  
  <p>My second problem is that I need to specify <a href="https://stackoverflow.com/questions/13022299/specify-date-format-for-colclasses-argument-in-read-table-read-csv">a custom coercion handler</a> for one of my data types, but that fails:</p>
  
  <pre><code>dt &lt;- fread(tfile, colClasses=list(date="myDate"))
  Error in fread(tfile, colClasses = list(date = "myDate")) : 
    Column name 'myDate' in colClasses not found in data
  </code></pre>
  
  <p>Yes, in the case of dates, a simple:</p>
  
  <pre><code>    dt[,date := as.Date(as.character(date), format="%Y%m%d")]
  </code></pre>
  
  <p>works.</p>
  
  <p>However, I have a different use case, which is to strip the decimal point from one of the data columns before it is converted from a character. Precision here is extremely important (thus our need for using the integer type), and coercing to an integer from the double type results in lost precision.</p>
  
  <p>Now, I can get around this with some system() calls to append the files and pipe them through some sed magic (simplified here) (where tfile is another temporary file):</p>
  
  <pre><code>if (has_header) {
      tfile2 &lt;- tempfile()
      system(paste("echo fakeline &gt;&gt;", tfile2))
      system(paste("head -q -n1", files[[1]], "&gt;&gt;", tfile2))
      system(paste("tail -q -n+2", tfile2, paste(files, collapse=" "),
                   " | sed 's/\\.//' &gt;&gt;", tfile), wait=wait)
      unlink(tfile2)
  } else {
      system(paste("cat", paste(files, collapse=" "), "&gt;&gt;", tfile), wait=wait)
  }
  </code></pre>
  
  <p>but this involves an extra read/write cycle. I have 4 TiB of data to process, which is a LOT of extra reading and writing (no, not all into one data.table. About 1000 of them.)</p>
  
  <h2>4. <code>fread</code> thinks named pipes are empty files</h2>
  
  <p>I typically leave wait=TRUE. But I was trying to see if I could avoid the extra read/write cycle by making tfile a named pipe <code>system('mkfifo', tfile)</code>, setting wait=FALSE, and then running fread(tfile). However, fread complains about the pipe being an empty file:</p>
  
  <pre><code>system(paste("tail -q -n+2", tfile2, paste(files, collapse=" "),
               " | sed 's/\\.//' &gt;&gt;", tfile), wait=FALSE)
  move &lt;- fread(tfile)
  Error in fread(tfile) : File is empty: /tmp/RtmpbxNI1L/file78a678dc1999
  </code></pre>
  
  <p>In any case, this is a bit of a hack.</p>
  
  <h2>Simplified Code if I had my wish list</h2>
  
  <p>Ideally, I would be able to do something like this:</p>
  
  <pre><code>setClass("Int_Price")
  setAs("character", "Int_Price",
      function (from) {
          return(as.integer(gsub("\\.", "", from)))
      }
  )
  
  dt &lt;- fread(files, colClasses=list(price="Int_Price"))
  </code></pre>
  
  <p>And then I'd have a nice long <code>data.table</code> with properly coerced data.</p>