[R] Use of library(X) in the code of library X.

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jun 6 20:42:19 CEST 2014


On 06/06/2014 10:26 AM, Bart Kastermans wrote:
> To improve my R skills I try to understand some R code written by others.  Mostly
> I am looking at the code of packages I use.  Today I looked at the code for the
> caret package
>
> http://cran.r-project.org/src/contrib/caret_6.0-30.tar.gz
>
> in particular at the file R/adaptive.R
>
> This file starts with:
>
> adaptiveWorkflow <- function(x, y, wts, info, method, ppOpts, ctrl, lev,
>                               metric, maximize, testing = FALSE, ...) {
>    library(caret)
>    loadNamespace("caret”)
>
>  From ?library and googling I can’t figure out what this code would do.
>
> Why would you call library(caret) in the caret package?

I don't know that package, and since adaptiveWorkflow is not documented 
at the user level, I can't tell exactly what the author had in mind.  
However, code like that could be present for debugging purposes (and is 
unintentionally present in the CRAN copy), or could be intentional.  The 
library(caret) call has the effect of ensuring that the package is on 
the search list.  (It might have been loaded invisibly by another 
package.)   This is generally considered to be bad form nowadays; 
packages should function properly without being on the search list.

I can't think of a situation where loadNamespace() would do anything --- 
it would have been called by library().

Duncan Murdoch



More information about the R-help mailing list