title: How do I override a non-visible function in the package namespace?
body: <p>I basically want to change a non-visible function of a package. For visible functions, i.e. functions that have no asterix when <code>methods</code> is called on them, I found two posts how I could achieve my goal:</p>
  
  <ol>
  <li>Use <code>assignInNamespace</code>: see post on <a href="https://stat.ethz.ch/pipermail/r-help/2008-August/171215.html" rel="noreferrer">R-help</a>.</li>
  <li>Use <code>fix</code>: see post on <a href="https://stackoverflow.com/questions/2458013/what-ways-are-there-to-edit-a-function-in-r">stackoverflow</a></li>
  </ol>
  
  <p>Although both approaches work for an exported / visible function (I use <code>predict.lm</code> as an example further below for the second approach and tested the first approach with the function <code>subset.data.frame</code>), they do not work for a non-visible function, e.g. <code>predict.ar</code>. Why is that? Is there a workaround?</p>
  
  <p>Here is a minimal example:</p>
  
  <p>Show that predict.lm is visible, predict.ar is not:</p>
  
  <pre><code>methods(predict)
   [1] predict.Arima*             predict.HoltWinters*       predict.StructTS*         
   [4] predict.ar*                predict.arima0*            predict.glm               
   [7] predict.lm                 predict.loess*             predict.mlm               
  [10] predict.nls*               predict.poly               predict.ppr*              
  [13] predict.prcomp*            predict.princomp*          predict.smooth.spline*    
  [16] predict.smooth.spline.fit*
  </code></pre>
  
  <p>Apply <code>predict.lm</code>:</p>
  
  <pre><code>x &lt;- rnorm(5)
  y &lt;- x + rnorm(5)
  predict(lm(y ~ x))
  #          1          2          3          4          5 
  #  1.0783047  1.5288031  0.3268405  0.8373520 -0.9833746
  </code></pre>
  
  <p>Change <code>predict.lm</code> by entering cat("First line changed for predict.lm\n") 
  at the beginning of the function body. (You have to do that manually in the editor):</p>
  
  <pre><code>fix(predict.lm)
  predict(lm(y ~ x))
  # First line changed for predict.lm
  #          1          2          3          4          5 
  #  1.0783047  1.5288031  0.3268405  0.8373520 -0.983374
  </code></pre>
  
  <p>Apply <code>predict.ar</code>:</p>
  
  <pre><code>sunspot.ar &lt;- ar(sunspot.year)
  predict(sunspot.ar, n.ahead=25)
  # $pred
  # Time Series:
  # Start = 1989 
  # End = 2013 
  </code></pre>
  
  <p>Try to change <code>predict.ar</code>:</p>
  
  <pre><code>fix(predict.ar) #Here, an empty function body appears for me
  fix("stats:::predict.ar") #Here as well
  fix(stats:::predict.ar)
  #Error in fix(stats:::predict.ar) : 'fix' requires a name
  </code></pre>
  
  <p>Try to use <code>assignInNamespace</code> instead. (Note that I just copied the function <code>stats:::predict.ar</code> in an editor and added the line <code>cat("First line changed for predict.ar\n")</code> at the beginning of the body. Because the function body is quite long, I only show the first couple of lines here)</p>
  
  <pre><code>mypredict &lt;- function (object, newdata, n.ahead = 1, se.fit = TRUE, ...) 
  {
      cat("First line changed for predict.ar\n")
      if (n.ahead &lt; 1) 
          stop("'n.ahead' must be at least 1")
      #Rest of body of stats:::predict.ar
  }
  assignInNamespace("predict.ar", mypredict, ns="stats")
  predict(sunspot.ar, n.ahead=25)
  # First line changed for predict.ar
  # Error in predict.ar(sunspot.ar, n.ahead = 25) : 
  #   object 'C_artoma' not found
  </code></pre>
  
  <p>Since "First line changed for predict.ar" is actually printed to the console, predict.ar must have been changed. However, why is the object 'C_artoma' not found anymore?</p>
  
  <p>UPDATE: OK, this is super embarrassing, but I can't delete that post anymore: The answer was already at the link I provided with Richie Cotton's answer at the end. Sorry for wasting your time! I think I checked everything and then I don't see the obvious. Could someone just post this as an answer an I accept it. Sorry again.</p>
  
  <pre><code>fixInNamespace(predict.ar, pos="package:stats")
  </code></pre>