[Rd] unloadNamespace

Gabriel Becker gmbecker at ucdavis.edu
Fri Jan 9 18:34:59 CET 2015


Paul,

In the version of R I had trivially available, the flushSession function
from switchr seems to perform as expected when tseries is loaded.

> library(tseries)
library(tseries)

    ‘tseries’ version: 0.10-32

    ‘tseries’ is a package for time series analysis and computational
    finance.

    See ‘library(help="tseries")’ for details.

> library(switchr)
library(switchr)
> sessionInfo()
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] switchr_0.4.7   tseries_0.10-32

loaded via a namespace (and not attached):
[1] BiocInstaller_1.16.1 bitops_1.0-6         grid_3.1.0
[4] lattice_0.20-29      quadprog_1.5-5       RCurl_1.95-4.5
[7] tcltk_3.1.0          tools_3.1.0          zoo_1.7-11
> flushSession()
flushSession()
NULL
Warning message:
In flushSession() : Unable to unload all namespaces
> sessionInfo()
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] switchr_0.4.7

loaded via a namespace (and not attached):
[1] BiocInstaller_1.16.1 bitops_1.0-6         grid_3.1.0
[4] RCurl_1.95-4.5       tcltk_3.1.0          tools_3.1.0
>

Ignore the warning in the middle there, it is referring to BiocInstaller.
The rest of the packages which remain loaded are base packages (which it
doesn't attempt to unload by default, as doing so is dangerous).

I'll try to run the same test on the version of R you were using soon, but
I don't have it handy atm.

~G

On Fri, Jan 9, 2015 at 9:09 AM, Winston Chang <winstonchang1 at gmail.com>
wrote:

> It's probably because the first thing that unloadNamespace does is this:
>    ns <- asNamespace(ns, base.OK = FALSE)
>
> If you call asNamespace("tseries"), it calls getNamespace("tseries"), which
> has the side effect of loading that package (and its dependencies).
>
> One way to work around this is to check loadedNamespaces() before you try
> to unload a package.
>
> -Winston
>
> On Thu, Jan 8, 2015 at 9:45 AM, Paul Gilbert <pgilbert902 at gmail.com>
> wrote:
>
> > In the documentation the closed thing I see to an explanation of this is
> > that ?detach says "Unloading some namespaces has undesirable side
> effects"
> >
> > Can anyone explain why unloading tseries will load zoo? I don't think
> this
> > behavior is specific to tseries, it's just an example. I realize one
> would
> > not usually unload something that is not loaded, but I would expect it to
> > do nothing or give an error. I only discovered this when trying to clean
> up
> > to debug another problem.
> >
> > R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
> > and
> > R Under development (unstable) (2015-01-02 r67308) -- "Unsuffered
> > Consequences"
> > ...
> > Type 'q()' to quit R.
> >
> > > loadedNamespaces()
> > [1] "base"      "datasets"  "graphics"  "grDevices" "methods"   "stats"
> > [7] "utils"
> > > unloadNamespace("tseries") # loads zoo ?
> > > loadedNamespaces()
> >  [1] "base"      "datasets"  "graphics"  "grDevices" "grid" "lattice"
> >  [7] "methods"   "quadprog"  "stats"     "utils"     "zoo"
> > >
> >
> > Somewhat related, is there an easy way to get back to a "clean" state for
> > loaded and attached things, as if R had just been started? I'm trying to
> do
> > this in a vignette so it is not easy to stop and restart R.
> >
> > Paul
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker, PhD
Alumnus
Statistics Department
University of California, Davis

	[[alternative HTML version deleted]]



More information about the R-devel mailing list