19

According to the R News for v2.14:

All packages must have a namespace, and one is created on installation if not supplied in the sources. This means that any package without a namespace must be re-installed under this version of R (but data-only packages without R code can still be used).

How do I programatically detect which packages installed under 2.13.x don't have a namespace so I know what needs to be updated?

Suraj
  • 35,905
  • 47
  • 139
  • 250
  • By the way, I can't simply call the update package function in my environment because my xcopy-deployable version of R is checked-into my source server and I use currently use SVN which makes it impossible to use the R update functions. So yes, I'm really looking for a way to detect packages without namespaces. – Suraj Nov 03 '11 at 16:35

3 Answers3

22

The function packageHasNamespace holds the key. Use it together with installed.packages:

The following code loops through all of the library locations in .libPaths:

pkgNS <- NULL
for(i in seq_along(.libPaths())){
  libLoc <- .libPaths()[i]
  pkgs <- installed.packages(lib.loc=libLoc)[, 1]
  pkgNS <- c(pkgNS, 
      sapply(unname(pkgs), packageHasNamespace, package.lib=libLoc)
  )
}

The result of this code is a named logical vector pkgNS that is TRUE if the package has a namespace, FALSE if it doesn't.

To get only those packages that don't have a namespace, create a subset of pkgNS where pkgNS is FALSE:

pkgNS[!pkgNS]

      abind      bitops   CircStats    combinat     corpcor      deldir 
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE 
     Design         evd   financial         fpc      getopt      gsubfn 
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE 
       ineq       magic     mlbench    optparse     plotrix       ppcor 
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE 
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • I made a correction to the original code. It should be `i in seq_along(.libPaths())` – Andrie Nov 03 '11 at 17:46
  • NP. Let me know if it doesn't work. Each time I run this function it seems something else stops working. In my latest incarnation I need to `unlist` the result of `sapply`. Good luck. – Andrie Nov 03 '11 at 21:00
5

Just run :

update.packages(checkBuilt=TRUE)
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
  • Very nice answer, but this will give a list of all packages that haven't been build in R2.14 - not just those that don't have a namespace. Most of the packages I use daily have had namespaces for quite some time. – Andrie Nov 03 '11 at 16:57
  • @Andrie Good point. No harm to refresh everything to be on safe side, though (R2.14 is quite a bit different). – Matt Dowle Nov 03 '11 at 17:36
  • I agree with you. It's just that the OP said he can't do that on his installation. – Andrie Nov 03 '11 at 17:37
  • Ah. Ok I see OP's comment now. – Matt Dowle Nov 03 '11 at 18:41
3

Great thread. I was stuck on the same problem. To finish all that needs to be done you can:

remove.packages(names(pkgNS[!pkgNS]))
install.packages(names(pkgNS[!pkgNS]))