>>>>> Michael Chirico
>>>>> on Thu, 23 Jul 2026 14:21:13 -0700 writes:
> Consider the following (currently {caret} Depends on {ggplot2} and
{lattice}):
> NB: in the below I set R_DEFAULT_PACKAGES=methods just to reduce visual
noise
> search()
> # [1] ".GlobalEnv" "package:methods" "Autoloads" "package:base"
> library(data.table)
> library(caret, pos="package:base")
> search()
> # [1] ".GlobalEnv" "package:lattice" "package:ggplot2"
> # [4] "package:data.table" "package:caret" "package:methods"
> # [7] "Autoloads" "package:base"
> c.f. a session where we attach the Depends ourselves:
> library(data.table)
> library(lattice, pos='package:base')
> library(ggplot2, pos='package:base')
> library(caret, pos='package:base')
> search()
> # [1] ".GlobalEnv" "package:data.table" "package:methods"
> # [4] "Autoloads" "package:lattice" "package:ggplot2"
> # [7] "package:caret" "package:base"
> I.e., the search() resolution of the Depends w.r.t. {data.table}
> differs in the two invocations.
> This is a bit surprising to me -- add it to the list of reasons why I
> think 'Depends' should be deprecated.
> For now, is this a bug?
What exactly? As you know, the default of `pos` is `pos = 2`
and that happens in the first of your cases.
Also, in your 2nd case (manually attach others before attaching caret)
also everything works (sequentially) as you asked for via
pos="package:base".
So definitely no bug.
But maybe a missing feature? -- It seems to me you'd like to get
an argument 'pos.all = ' which would become the 'pos' argument
for all attach()es of the dependent packages, .. would it?
Or maybe even more flexibly a version of library()
where you can directly specify several packages which should all
be attached to search() *and* will end up being both
_contiguous_ *and* in the same order as you specify them in your [say] use()
call.
**BUT** your wish seems to have been fulfilled already more than 2 years ago:
The NEWS since R 4.5.0 contain
• New function use() to use packages in R scripts with full control
over what gets added to the search path. (Actually already
available since R 4.4.0.)
and if the claim is correct, this is indeed possible for 2.25 years :
$ R-4.4.0 --version |head -1
R version 4.4.0 (2024-04-24) -- "Puppy Cup"
Note that to get the correct fulfillment of wish above {get them
in *my* order} would work like this
R_DEFAULT_PACKAGES=methods R_PROFILE= R -q -e
'myPkgs<-c("data.table","caret","lattice","ggplot2");invisible(lapply(rev(myPkgs),use));search()'
> myPkgs<-c("data.table","caret","lattice","ggplot2");invisible(lapply(rev(myPkgs),use));search()
Attaching package: ‘data.table’
The following object is masked from ‘package:base’:
%notin%
[1] ".GlobalEnv" "package:data.table" "package:caret"
[4] "package:lattice" "package:ggplot2" "package:methods"
[7] "Autoloads" "package:base"
>
This works almost as you want it; ideally, I think we should be
allowed to pass 'pos = .' to use().
Best,
Martin
> An existing workaround is to use
> attach.required=FALSE, but it's not ideal -- it has the similar effect
> of requiring the author to track down recursive Depends to attach them
> manually.
> Michael Chirico
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel