[R] Automatic detachment of dependent packages

Duncan Murdoch murdoch at stats.uwo.ca
Fri Sep 7 20:25:06 CEST 2007


On 9/7/2007 2:05 PM, Paul Smith wrote:
> On 9/7/07, Barry Rowlingson <b.rowlingson at lancaster.ac.uk> wrote:
>> > When one loads certain packages, some other dependent packages are
>> > loaded as well. Is there some way of detaching them automatically when
>> > one detaches the first package loaded? For instance,
>> >
>> >> library(sqldf)
>> > Loading required package: RSQLite
>> > Loading required package: DBI
>> > Loading required package: gsubfn
>> > Loading required package: proto
>> >
>> > but
>> >
>> >> detach(package:sqldf)
>> >>
>> >> search()
>> >  [1] ".GlobalEnv"        "package:gsubfn"    "package:proto"
>> >  [4] "package:RSQLite"   "package:DBI"       "package:stats"
>> >  [7] "package:graphics"  "package:grDevices" "package:utils"
>> > [10] "package:datasets"  "package:methods"   "Autoloads"
>> > [13] "package:base"
>> >
>> > The packages
>> >
>> > RSQLite
>> > DBI
>> > gsubfn
>> > proto
>> >
>> > were not detached.
>>
>>   The danger here is that after attaching sqldf you might attach some
>> other package that needs, say, DBI, then when your cleanup routine
>> detaches DBI that other package dies because DBI isn't there.
>>
>>   The way to do it would be to detach any packages that are only
>> depended on by the package you are detaching. You'd have to call
>> packageDescription("foo", fields="Depends") for currently attached
>> packages to build the dependency tree and then work out which ones you
>> can remove... There's a bit of recursive tree-walking in there, but it
>> should be simple... Ummm...
> 
> Thanks, Barry and Gabor. Please, look at the following:
> 
>> library(sqldf)
> Loading required package: RSQLite
> Loading required package: DBI
> Loading required package: gsubfn
> Loading required package: proto
>> packageDescription("sqldf", fields="Depends")
> [1] "R (>= 2.5.1), RSQLite (>= 0.5-5), gsubfn"
>>
> 
> packageDescription does not mention the packages DBI and proto.

They are part of the recursive tree-walking that Barry mentioned, i.e. 
dependencies of RSQLite or gsubfn:

 > packageDescription("RSQLite", fields="Depends")
[1] "R (>= 2.3.0), methods, DBI (>= 0.2-3)"
 > packageDescription("gsubfn", fields="Depends")
[1] "R (>= 2.4.0), proto"

But you can get R to do the walking for you, using the pkgDepends 
function from the tools package:

 > pkgDepends("sqldf")
$Depends
[1] "DBI (>= 0.2-3)"     "gsubfn"             "methods"
[4] "proto"              "RSQLite (>= 0.5-5)"

... [ more stuff deleted ] ...

Watch out though:  you may not want to detach "methods" when you detach 
"sqldf", because it was already there.  Essentially you need some sort 
of snapshot of the state beforehand, and then you could restore it.

Duncan Murdoch



More information about the R-help mailing list