title: Overriding a package function inherited by another package
body: <p>I am trying to override the <code>tidy.source</code> function of the <code>knitr</code> package. The problem is that <code>tidy.source</code> is defined in the <code>formatR</code> package, which is imported by the <code>knitr</code> package.
  If I run: </p>
  
  <p><code>get("tidy.source", envir=asNamespace("knitr"))</code> </p>
  
  <p>I get the original code. 
  So I am tempted to override <code>tidy.source</code> with:</p>
  
  <p><code>assignInNamespace ("tidy.source", function()print("My tidy.source"),  "knitr")</code>, </p>
  
  <p>but I get:</p>
  
  <p><code>Error in bindingIsLocked(x, ns) : no binding for "tidy.source"</code>.</p>
  
  <p>In fact <code>tidy.source</code> is defined in <code>formatR</code> and inherited by <code>knitr</code>.  With:</p>
  
  <p><code>assignInNamespace ("tidy.source", function()print("My tidy.source"),  "formatR")</code></p>
  
  <p>everything is apparently smooth, but checking again <code>get("tidy.source", envir=asNamespace("knitr"))</code> shows that inside <code>knitr</code> nothing has changed.</p>
  
  <p>Any help? </p>
  
  <h3>EDIT:</h3>
  
  <p>This question is partly obsolete due to the new development release of <em>knitr/formatR</em>. Many thanks to Yihui for noticing this discussion and for deciding to update his package. See:</p>
  
  <p><a href="https://github.com/yihui/formatR/commit/6f70360f359caa8d2bb33190a1c89530defb0e98" rel="nofollow">https://github.com/yihui/formatR/commit/6f70360f359caa8d2bb33190a1c89530defb0e98</a></p>
  
  <p>I can definitely switch from <em>Sweave</em> to <em>knitr</em>. </p>
  
  <p>The general question concerning the overriding of an imported package function remains anyway open. Since it is not related to <em>knitr/formatR</em> package anymore, I restate it in more general terms.</p>
  
  <p>Suppose you have a package <em>main</em> importing the package <em>imp</em>. If you load the former, <code>"package:main"</code> shows in the list of the attached packages and both <code>"main"</code> and <code>"sub"</code> show among the names of the loaded namespaces. </p>
  
  <p>Assume that <em>main</em> imports the exported <em>sub</em> function <em>exp.sub.func</em>, which calls in turns the non-exported <em>sub</em> function <em>prv.sub.func</em>.  If you want to change/customise <em>exp.sub.func</em> with your <em>exp.sub.func.mod</em>, you may think of using: </p>
  
  <pre><code>assign("exp.sub.func", exp.sub.func.mod, asNamespace ("sub"))
  </code></pre>
  
  <p>As a result, by running <code>sub::exp.sub.func</code> you will get your patched version (that is <em>exp.sub.func.mod</em>).<br>
  Unfortunately, as far as your <em>exp.sub.func.mod</em> keeps on relying on <em>prv.sub.func</em>, you get the error:   </p>
  
  <pre><code>Error in [...] : object 'prv.sub.func' not found
  </code></pre>
  
  <p>In fact:</p>
  
  <pre><code>environment(sub::exp.sub.func) 
  </code></pre>
  
  <p>returns now: <code>&lt;environment: R_GlobalEnv&gt;</code> while it was <code>&lt;environment: namespace:sub&gt;</code> before patching. </p>
  
  <p>The question is: <em>how to move the patched function to the proper namespace</em>? </p>
  
  <p>To implement the problem above you can use whatever packages of course; in my case I used <em>knitr</em> and <em>formatR</em> as main and imported namespace and <em>tidy.source()</em> as patched function. </p>