romainfrancois / nothing

A package about nothing
40 stars 11 forks source link

self detach #1

Closed romainfrancois closed 10 years ago

romainfrancois commented 10 years ago

It would be nice if nothing would detach itself. Not sure how to do that.

romainfrancois commented 10 years ago

I tried :

reg.finalizer( environment(), function(){
   detach( "package:nothing" )
})

in the .onLoad function, with no success. or perhaps the environment did not get gc'ed yet.

wch commented 10 years ago

This seems to unload self. You need to trick it into not doing this during installation, or installation will fail:

.onAttach <- function(libname, pkgname) {
  # Don't unload self when being installed - that will result in install error
  if (Sys.getenv("R_INSTALL_PKG") != "nothing") {
    unloadNamespace("nothing")
  }
}
wch commented 10 years ago

Actually, this code results in an error during library(nothing). But it works.

wch commented 10 years ago

Update: this seems to work without errors:

.onLoad <- function(libname, pkgname){
  if (Sys.getenv("R_INSTALL_PKG") == "nothing")
    return()

  pkgs <- setdiff(loadedNamespaces(), "base")
  while (length(pkgs) > 0) {
    for (pkg in pkgs) {
      try(unloadNamespace(pkg), silent = TRUE)
    }

    pkgs <- setdiff(loadedNamespaces(), "base")
  }
}
leeper commented 10 years ago

Perhaps an easier way to approach this is not loading the package at all, but instead having the package consist only of a single, non-exported function. Thus all the legwork is done with nothing:::nothing() instead of require(nothing). Just an idea.

romainfrancois commented 10 years ago

@leeper the essence of the package is that it gives meaning to the expression require(nothing). That's the only acceptable syntax ;)