[Rd] odd assignInNamespace / setGeneric interaction

William Dunlap wdunlap at tibco.com
Wed Apr 18 22:38:06 CEST 2018


A coworker got tired of having to type 'yes' or 'no' after quitting R: he
never wanted to save the R workspace when quitting.  So he added
assignInNamespace lines to his .Rprofile file to replace base::q with
one that, by default, called the original with save="no"..

  utils::assignInNamespace(".qOrig", base::q, "base")
  utils::assignInNamespace("q", function(save = "no", ...)
base:::.qOrig(save = save, ...), "base")

This worked fine until he decide to load the distr package:

  > suppressPackageStartupMessages(library(distr))
  Error: package or namespace load failed for ‘distr’ in
loadNamespace(name):
   there is no package called ‘.GlobalEnv’

distr calls setGeneric("q"), which indirectly causes the environment
of base::q, .GlobalEnv, to be loaded as a namespace, causing the error.
Giving his replacement q function the environment getNamespace("base")
avoids the problem.

I can reproduce the problem by making a package that just calls
setGeneric("as.hexmode",...) and a NAMEPACE file with
exportMethods("as.hexmode").  If my .Rprofile puts a version of as.hexmode
with environment .GlobalEnv into the base namespace, then I get the same
error when trying to load the package.

I suppose this is mostly a curiosity and unlikely to happen to most people
but it did confuse us for a while.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

	[[alternative HTML version deleted]]



More information about the R-devel mailing list