Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I determine the namespace import order in [R]
    text
    copied!<p>I need to clean up an R instance to return it to its on-launch virginal state. So far, what I'm doing is:</p> <p>On launch, record the loaded packages and namespaces</p> <pre><code>original_packages &lt;- grep('^package:', search(), value = TRUE) original_namespaces &lt;- loadedNamespaces() </code></pre> <p>When I need to flush the instance, detach each loaded package that was not there at launch:</p> <pre><code>for (pkg in grep('^package:', search(), value = TRUE)) { if (! pkg %in% original_packages){ detach(pkg, unload=TRUE, force=TRUE, character.only=TRUE) } } </code></pre> <p>The problem is that if I have loaded a package with a bunch of imported namespaces, such as ggplot2, those namespaces stay loaded, and I have to unload them in order of import, from high-level down. Just unloading them blindly doesn't work, as I get "namespace ‘x’ is imported by ‘y’, ‘z’ so cannot be unloaded" errors.</p> <p>Here is reproducible example:</p> <pre><code>original_packages &lt;- grep('^package:', search(), value = TRUE) original_namespaces &lt;- loadedNamespaces() library(ggplot2) library(plyr) loadedNamespaces() for (pkg in grep('^package:', search(), value = TRUE)) { if (! pkg %in% original_packages){ detach(pkg, unload=TRUE, force=TRUE, character.only=TRUE) } } for (ns in loadedNamespaces()) { if (! ns %in% original_namespaces){ unloadNamespace(ns) } } </code></pre> <p>Is there some way to figure out the namespace import hierarchy? If so, then I should just be able to order the last loop correctly...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload