R/unload_pac.R

##-------------------------------------------------------------
## 
##-------------------------------------------------------------
unload_pac = function(..., 
					  except = c("rj", "bibliotheca", "nvimcom", "colorout"), 
					  info = FALSE, 
					  verbose = TRUE) 
{
	## todos os pacotes fixados 
	#si.pkgs = paste0("package:", names(sessionInfo()$otherPkgs))
	si.pkgs = names(utils::sessionInfo()$otherPkgs)
	#si.pkgs = loadedNamespaces()
	
	if(missing(...))
	{
		#except = paste0("package:", except)
		#si.pkgs = paste0("package:", names(sessionInfo()$otherPkgs))
		#si.pkgs = names(sessionInfo()$otherPkgs)
		
		si.pkgs = si.pkgs[!si.pkgs %in% except]
		
		if(length(si.pkgs)) detach_pac(si.pkgs) 
		else si.pkgs = "nothing to detach"
		
		if(verbose) mensagem(si.pkgs, msg = 5)
		
		if(info) return(utils::sessionInfo())
		
		return(invisible(si.pkgs))
	}#end if
	
	
	## nomes dos pacotes como vetor
	v = tryCatch(is.vector(...), error = function(e)return(FALSE))
	## nomes dos pacotes
	if(v) pkgs = as.vector(...) else 
		pkgs = as.character(match.call(expand.dots = FALSE)[[2]])#end if
	
	## pacotes fixados
	pkgs.on = si.pkgs[si.pkgs %in% pkgs]
	## pacotes não fixados
	pkgs.off = pkgs[!pkgs %in% pkgs.on]
	
	if(length(pkgs.on))
	{	## desfixa pacote		
		detach_pac(pkgs.on)
		if(verbose) mensagem(pkgs.on, msg = 5)
	}#end if
	
	if(length(pkgs.off))
	{## pacotes não fixados
		if(verbose) mensagem(pkgs.off, aviso = "Package not attached")
	}#end if		
	
	if(info) return(utils::sessionInfo())
	
	return(invisible(TRUE))
	#return(pkgs.on)
	
}#end function unload_pac


detach_pac = function(pkgs)
{	
	#deparse(substitute(db))
	## pkgs tem que ser package:nome
	pkgs = paste("package", pkgs, sep=":")
	lapply(pkgs, detach, character.only = TRUE, unload = TRUE, force = TRUE)
	#try(unloadNamespace(pkg$package), silent = TRUE)
	
	#lapply(pkgs, unloadNamespace)
	
#	if (!is.null(.getNamespace(pkg$package))) {
#		message("unloadNamespace(\"", pkg$package,
#				"\") not successful, probably because another loaded package depends on it.",
#				"Forcing unload. If you encounter problems, please restart R.")
#		unregister_namespace(pkg$package)
#	}
	
}#end function detach_pac
salah31416/bibliotheca documentation built on June 16, 2019, 10:02 p.m.