title: How to write a raster with RAT factors in R raster package
body: <p>I want <code>writeRaster</code> to write the RAT (raster attribute table) that I've built in R.</p>
  
  <p>I'm running R 3.0.1, raster 2.1-49, and rgdal 0.8-10.</p>
  
  <p>My input raster looks like this:</p>
  
  <pre><code>r &lt;-raster("F:/test.img")
  
  class       : RasterLayer 
  dimensions  : 3, 3, 9  (nrow, ncol, ncell)
  resolution  : 30, 30  (x, y)
  extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
  coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
  data source : F:\test.img 
  names       : test 
  values      : 1, 19  (min, max)
  </code></pre>
  
  <p>I then build my attribute table:</p>
  
  <pre><code>r &lt;- ratify(r)
  rat &lt;- levels(r)[[1]]
  rat$Pixel_Values &lt;- c(1, 7, 8, 9, 19)
  rat$Class_Names &lt;- c("A", "B", "C", "D", "E")
  levels(r) &lt;- rat
  </code></pre>
  
  <p>Which results in a <code>raster</code> with attributes:</p>
  
  <pre><code>r
  
  # class       : RasterLayer 
  # dimensions  : 3, 3, 9  (nrow, ncol, ncell)
  # resolution  : 30, 30  (x, y)
  # extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
  # coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
  # data source : F:\test.img 
  # names       : test 
  # values      : 1, 19  (min, max)
  # attributes  :
  #  ID Pixel_Values Class_Names
  #   1            1           A
  #   7            7           B
  #   8            8           C
  #   9            9           D
  #  19           19           E
  </code></pre>
  
  <p>I then attempt to write my raster together with its RAT:</p>
  
  <pre><code>ratRaster &lt;- "F:/testRat.img"
  writeRaster(r, filename=ratRaster, datatype="INT1U", RAT=TRUE, progress="window", overwrite=TRUE)
  </code></pre>
  
  <p>But when I read it back into R, it becomes apparent that the attributes did not persist:</p>
  
  <pre><code>r2 &lt;- raster(ratRaster)
  
  r2
  # class       : RasterLayer 
  # dimensions  : 3, 3, 9  (nrow, ncol, ncell)
  # resolution  : 30, 30  (x, y)
  # extent      : 347325, 347415, 4301655, 4301745  (xmin, xmax, ymin, ymax)
  # coord. ref. : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
  # data source : F:\testRat.img 
  # names       : testRat 
  # values      : 1, 19  (min, max)
  </code></pre>
  
  <p>It would be quick and awesome to build RATs in R. How can I create and export the raster and keep the RAT? </p>