Inspired by this answer I am looking for a way to detach several packages at once.
When I load say Hmisc,
# install.packages("Hmisc", dependencies = TRUE)
require(Hmisc)
R
also loads survival
and splines
. My question is if there is a way to unload that group together?
I currently do something like this,
detach(package:Hmisc, unload = T)
detach(package:survival, unload = T)
detach(package:splines, unload = T)
I tried,
detach(package:c('Hmisc', 'survival', 'splines'), unload = T)
…
To delete all currently attached packages:
?detach
explicitly rules out supplying a character vector (as opposed to scalar, ie more than one library to be detached) as its first argument, but you can always make a helper function. This will accept multiple inputs that can be character strings, names, or numbers. Numbers are matched to entries in the initial search list, so the fact that the search list dynamically updates after each detach won't cause it to break.The messing with
eval(substitute(...
is necessary because, unlesscharacter.only=TRUE
,detach
handles its first argument in a nonstandard way. It checks if it's a name, and if so, usessubstitute
anddeparse
to turn it into character. (Thecharacter.only
argument is misnamed really, asdetach(2, character.only=TRUE)
still works. It should really be called "accept.names" or something.)Another option:
To answer my own question to Hong's answer:
Works just fine. The sorting function at the top of
base::detach
is a bit wonky, but usingcharacter.only=TRUE
got me thru just fine.