title: R CMD INSTALL is stuck on a function that works... how can I debug this further?
body: <p><em>note:</em> much of the information in the problem statement is irrelevant, and the problem has been resolved with no apparent reason through the debugging steps that are listed in the <strong>resolution</strong> section</p>
  
  <p><em>I apologize for asking a question about an error that I can not reproduce.</em></p>
  
  <p><strong>Problem</strong></p>
  
  <p>I have a test function 'foo.R' in my package 'PKG'; foo.R consists of:</p>
  
  <pre><code>foo &lt;- function (filename, n) {
    text &lt;- scan(file = filename, what = "character")
    if (n==1) text &lt;- gsub("\\#GGG", '\\#', text)
    if (n&gt;1)  text &lt;- gsub("\\#GGG", '', text)
    writeLines(text, con = 'newfn.R')
  }
  </code></pre>
  
  <p>The intent of foo is to either uncomment lines by replacing "#GGG" with "" when n>1 or to leave the line commented if n == 1</p>
  
  <p>The minimal code required to produce this error is:</p>
  
  <pre><code>foo &lt;- function (string) {
      gsub("\\#GGG", '', string)
  }
  </code></pre>
  
  <p>However, when I run <code>R CMD INSTALL PKG</code> I get the following error:</p>
  
  <pre><code>Error in parse(outFile) : 
    /tmp/RtmpLbFQF0/R.INSTALL2edd9a07/PKG/R/foo.R:3:1: unexpected '}'
  2:   gsub("\\#GGG", '\\#', string)
  3: }
     ^
  </code></pre>
  
  <p><strong>UPDATES</strong></p>
  
  <ol>
  <li><p>Based on the answers, '\#' is not the culprit.</p></li>
  <li><p>I have found that the problem is during install (not build), and I have changed the question to reflect this. I have also fixed the curly braces from my test case.</p></li>
  <li><p>Also, I have found that not only does the function work, e.g. when loaded with source(), </p>
  
  <ul>
  <li><p>When I make a new project (e.g. 'newproject') and move the function there, it installs without error, i.e. this works:</p>
  
  <ol>
  <li>move foo.R to newproject/R/foo.R</li>
  <li>make a newproject/DESCRIPTION </li>
  <li>R CMD build newproject</li>
  <li>R CMD INSTALL newproject</li>
  </ol></li>
  <li><p>This gives the error above:</p>
  
  <ol>
  <li>R CMD build PKG</li>
  <li>R CMD INSTALL PKG</li>
  </ol></li>
  </ul></li>
  <li><p>In response to the request for the error output log, the output from <code>R CMD check PKG</code> is the same as the error from the <code>R CMD INSTALL PKG</code>,</p></li>
  </ol>
  
  <blockquote>
    <ul>
    <li>installing <em>source</em> package ‘PKG’ ...
    ** R Error in parse(outFile) :    /home/user/PKG/R/foo.R:24:0:
    unexpected end of input 22:<br>
    writeLines(model.text, con = outfile)
    23: }    ^ ERROR: unable to collate
    files for package ‘PKG’</li>
    <li>removing ‘/home/user/PKG.Rcheck/PKG’</li>
    </ul>
  </blockquote>
  
  <p><strong>Resolution</strong></p>
  
  <p>The resolution of the error is inexplicable, but I reproduced the same seemingly magical method on two functions with the same error. </p>
  
  <ol>
  <li><code>mv /PKG/R/foo.R PKG/foo.R</code></li>
  <li>Visit new <code>PKG/R/foo.R</code> in emacs</li>
  <li>for (i in misc bits of function
  <ol>
  <li>cut-and-paste i</li>
  <li><code>R CMD check PKG</code></li>
  <li>if <code>PKG</code> can install</li>
  <li>return to <code>1</code></li>
  <li>else remove bits and return to <code>3</code></li>
  </ol></li>
  <li><code>diff PKG/foo.R PKG/R/foo.R</code></li>
  <li>if no difference except that function now works
  <ol>
  <li>update question on SO</li>
  <li>consider previous error a fluke</li>
  <li>continue as if nothing happened</li>
  </ol></li>
  </ol>
  
  <h2>UPDATE 2: culprit found!</h2>
  
  <p>The problem was an errant 'e' stuck to the left of the comment symbols ## coming before a function</p>