From winstonchang1 at gmail.com  Fri Jan 23 17:15:53 2015
From: winstonchang1 at gmail.com (Winston Chang)
Date: Fri, 23 Jan 2015 10:15:53 -0600
Subject: [Rd] speedbump in library
In-Reply-To: <CAGh0NYoAgB-ydT2j0eynAcRMefmL6C3m81dD-a6ZfDmN3kO+tw@mail.gmail.com>
References: <CAGh0NYoAgB-ydT2j0eynAcRMefmL6C3m81dD-a6ZfDmN3kO+tw@mail.gmail.com>
Message-ID: <CAFOpNVFT3hXcfbDk2fPV2ZyH=NonfZ-Nys05O4E1qWH_kJ8XAA@mail.gmail.com>

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.peter at 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.
> phaverty at gene.com
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] speedbump in library
Winston Chang