I think you can simplify a little by replacing this: pkg %in% loadedNamespaces() with this: .getNamespace(pkg)
Whereas getNamespace(pkg) will load the package if it's not already loaded, calling .getNamespace(pkg) (note the leading dot) won't load the package. I can't speak to whether there are any pitfalls in changing the library path searching, though. -Winston On Thu, Jan 22, 2015 at 12:25 PM, Peter Haverty <haverty.pe...@gene.com> wrote: > Hi all, > > Profiling turned up a bit of a speedbump in the library function. I > submitted a patch to the R bug tracker as bug 16168 and I've also > included it below. The alternate code is simpler and easier to > read/maintain, I believe. Any thoughts on other ways to write this? > > Index: src/library/base/R/library.R > =================================================================== > --- src/library/base/R/library.R (revision 67578) > +++ src/library/base/R/library.R (working copy) > @@ -688,18 +688,8 @@ > out <- character() > > for(pkg in package) { > - paths <- character() > - for(lib in lib.loc) { > - dirs <- list.files(lib, > - pattern = paste0("^", pkg, "$"), > - full.names = TRUE) > - ## Note that we cannot use tools::file_test() here, as > - ## cyclic namespace dependencies are not supported. Argh. > - paths <- c(paths, > - dirs[dir.exists(dirs) & > - file.exists(file.path(dirs, > - "DESCRIPTION"))]) > - } > + paths <- file.path(lib.loc, pkg) > + paths <- paths[ file.exists(file.path(paths, "DESCRIPTION")) ] > if(use_loaded && pkg %in% loadedNamespaces()) { > dir <- if (pkg == "base") system.file() > else getNamespaceInfo(pkg, "path") > > Pete > > ____________________ > Peter M. Haverty, Ph.D. > Genentech, Inc. > phave...@gene.com > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel