# TODO: Add comment
# 
# http://stackoverflow.com/questions/6979917/how-to-unload-a-package-without-restarting-r
# Author: edited Sep 2012 at 21:13 Richie Cotton, answered Aug 8 2011 at 9:19 kohske
###############################################################################



# How to unload a package without restarting R


# simple situation
detach("package:example.name", unload=TRUE)


# more complex situation
# It is possible to have multiple versions of a package loaded at once
# (for example, if you have a development version and a stable version in different libraries).
# To detach guarantee that all copies are detached, use this function:

detach_package <- function(pkg, character.only = FALSE){
	# Usage: detach_package(example.name)  or  detach_package("example.name", T)
	
	if(!character.only)
	{
		pkg <- deparse(substitute(pkg))
	}
	search_item <- paste("package", pkg, sep = ":")
	while(search_item %in% search())
	{
		detach(search_item, unload = TRUE, character.only = TRUE)
	}
}



