Wednesday, June 29, 2011

Using detach() to unload a library

Just a quick tidbit about unloading a library from the current R session using detach().

Say we have a current session of R open, and, lo and behold, there is a new version of "phytools" available. If we download the binaries and try to install, e.g.:

> install.packages("phytools_0.0-3.zip",repos=NULL)

we get the following error:

Warning: package 'phytools' is in use and will not be installed

This can be avoided simply by unloading the library before installing, and the reloading the new version afterwards. This is accomplished as follows:

> detach(package:phytools)
> install.packages("phytools_0.0-3.zip",repos=NULL)
package 'phytools' successfully unpacked and MD5 sums checked
> require(phytools)
Loading required package: phytools


Of course, this also applies to packages installed from a CRAN mirror.

3 comments:

  1. This a very old post, but I think this will not really unload the package unless you do:

    detach("package:phytools",unload=TRUE)

    ReplyDelete
  2. Hi Liam
    Very helpful post. I am trying to cut my tree as well.
    Here is the structure of my tree:
    str(nj_tree_root)
    List of 4
    $ edge : int [1:508, 1:2] 256 509 323 322 321 320 319 318 317 309 ...
    $ edge.length: num [1:508] 0 0 0 0 0 ...
    $ tip.label : chr [1:255] "hxAUG26up1s7g100t1" "hxAUG26us32g70t1" "hxAUG26rep1s3g123t1" "hxNCBI_GNO_60424" ...
    $ Nnode : int 254
    - attr(*, "class")= chr "phylo"
    - attr(*, "order")= chr "cladewise"

    When I use your program as follows:
    trees<-treeSlice(nj_tree,0.0)
    Error in if (node <= Ntip) stop("node number must be greater than the number of tips") :
    missing value where TRUE/FALSE needed
    I receive the above error.
    Any help is appreciated.
    Rocky

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.