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

Max Kuhn mxkuhn at gmail.com
Fri Jun 6 21:14:13 CEST 2014


That is legacy code but there was a good reason back then.

caret is written to use parallel processing via the foreach package.
There were some cases where the worker processes did not load the
required packages (even when I used foreach's ".packages" argument) so
I would do it explicitly. I don't recall which parallel backend had
the issue.

The more important lesson is that if you want to "understand some R
code written by others" you'll learn more bad habits than good ones if
you examine my packages…

Max

On Fri, Jun 6, 2014 at 2:42 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
> 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
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list