Namespace fails to unload in R

Tag: r , namespaces , onunload Author: lzdou Date: 2014-04-23

I am having trouble unloading the namespace for a package I created in R. Every time I try to do so I get the following error:

Error in .mergeMethodsTable(generic, mtable, get(tname, envir = env), : trying to get slot "defined" from an object of a basic class ("environment") with no slots
Calls: unloadNamespace ... -> .updateMethodsInTable -> .mergeMethodsTable

Here are the results of a call to traceback() after the above error occurs.

>4: .mergeMethodsTable(generic, mtable, get(tname, envir = env), attach)
 3: .updateMethodsInTable(fdef, where, attach)
 2: methods:::cacheMetaData(ns, FALSE, ns)
 1: unloadNamespace("coleXcms")

I honestly have tried everything I can think of, but to no avail. I'm pretty new to R so I was hoping someone might be able to help me.

Also, don't know if this will be useful, but here is my packages unloading hook. (The name of my package is coleXcms)

.onUnload <- function(libpath) {  
    library.dynam.unload("coleXcms", libpath)
It might help to provide the result of traceback() after the error occurs.
Updated post to show results of traceback @MartinMorgan

Other Answer1

The function I have used with success is unloadNamespace. It appears that library.dynam.unload is designed to remove the DLLs but it's not clear to me that it will remove the rest of a package.


The above error is what I get when I call detatch (package:coleXcms, unload=T) which I believe results in a call to unloadNamespace at some point. Correct me if I'm wrong.
You can attempt to verify your understanding by typing detach at the console. I think there are discussions in the R-help archives that make a distinction between the effects of detach and unloadNamepsace. My unconfirmed memory was that unloadNamespace may succeed when detach fails ... here's where I may have gathered that impression:… I am unable to explain why but can report from experience that unloadNamespace succeeds when detach fails.
I get the same error as above when calling unloadNamespace ...