title: R: what's the proper way to overwrite a function from a package?
body: <p>I am using a R package, in which there are 2 functions f1 and f2 (with f2 calling f1)<br>
  I wish to overwrite function f1.   </p>
  
  <p>Since R 2.15 and the mandatory usage of namespace in packages, if I just source the new function, it is indeed available in the global environement (ie. just calling f1(x) in the console returns the new result). However, calling f2 will still use the packaged function f1. (Because the namespace modifies the search path, and seals it as explained <a href="http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-namespaces" rel="noreferrer">here in the Writing R Extensions</a> tutorial)<br>
  What is the proper way to completely replace f1 with the new one? (apart from building again the package!) This can be useful in several situations. For instance if there is a bug in a package that you have not developed. Or if you don't want to re-build your packages everyday while they are still under development.</p>
  
  <p>I know about function </p>
  
  <pre><code>assignInNamespace("f1",f1,ns="mypackage")
  </code></pre>
  
  <p>However, the help page <code>?assignInNamespace</code> is a bit enignmatic and seems to discourage people from using it without giving more information, and I couldn't find any best practice recommendations on the official CRAN tutorial. and after calling this function:</p>
  
  <pre><code># Any of these 2 calls return the new function
  mypackage::f1 
  getFromNamespace(x = "f1", envir = as.environment("package:mypackage"))
  
  # while this one still returns the old packaged version
  getFunction(name = "f1", where = as.environment("package:mypackage")) 
  </code></pre>
  
  <p>This is very disturbing. How is the search path affected?</p>
  
  <p>For now I am doing some ugly things such as modifying the <code>lockEnvironment</code> function so that <code>library</code> doesn't lock the package namespace, and I can lock it at a later stage once I have replaced f1 (which seems really not a good practice)</p>
  
  <p>So basically I have 2 questions:</p>
  
  <ol>
  <li>what does exactly do <code>assignInNamespace</code> in the case of a package namespace (which is supposed to be locked)</li>
  <li>What are the good practices?</li>
  </ol>
  
  <p>many thanks for sharing your experience there.</p>
  
  <p>EDIT: people interested in this question might find <a href="http://obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/" rel="noreferrer">this blog post</a> extremely interesting.</p>