From hb at stat.berkeley.edu  Thu Feb 21 23:57:46 2008
From: hb at stat.berkeley.edu (Henrik Bengtsson)
Date: Thu, 21 Feb 2008 14:57:46 -0800
Subject: [Rd] Suggestion: isLoaded() to test if a package is loaded (without
	loading it)
Message-ID: <59d7961d0802211457s42e3ac82r6a2ea73af701ea82@mail.gmail.com>

Hi,

for 'affxparser' (Bioconductor), we needed a function to test if a
certain package was loaded or not, but we did not want to load it if
it wasn't, which is why we couldn't use require().  We came up with
the following solution:

  isPackageLoaded <- function(package, version=NULL, ...) {
    s <- search();
    if (is.null(version)) {
      s <- sub("_[0-9.-]*", "", s);
    } else {
      package <- manglePackageName(package, version);
    }
    pattern <- sprintf("package:%s", package);
    (pattern %in% s);
  }

Example:  isPackageLoaded("base")

As I see that require() and library() is using similar code for
testing if a package is loaded or not, feel free to have the above
added to the 'base' package.

/Henrik



[Rd] Suggestion: isLoaded() to test if a package is loaded (without loading it)
Henrik Bengtsson