>>>>> Trevor Davis writes:

Thanks.

This is really about what

  library("foo", include.only = "fun2")

should do if package 'foo' was already attached and the include.only
contradicts a previous specification.

In principle, one could look into allowing the underlying
attachNamespace() to add more exports into the package environment (the
'only' in 'include.only' might suggest differently, but we would have to
live with that).  However, using detach/attach is not the right way of
doing this, as it possibly modifies the search path and may trigger
hooks that should not be triggered.  Doing it right would probably need
an .Internal and hence be a lot of work ...

What definitely needs attention is the fact that currently the above is
silently ignored.  So at least for now we should teach attachNamespace()
to throw a suitably classed error if called with include.only that
contradicts a previous specification.

Contributions welcome :-)

Best
-k


>> Of course, it would be preferable if didn't need to detach the namespace.
> Here's a version only detaches the namespace when necessary:

> ```r
> use <- function(package, include.only) {
>     package <- as.character(package)[[1L]]
>     if (!requireNamespace(package, quietly = TRUE)) {
>         warning("there is no package called ", sQuote(package))
>         return (invisible(FALSE))
>     }

>     if (missing(include.only)) {
>         ns <- asNamespace(package, base.OK = FALSE)
>         include.only <- getNamespaceExports(ns)
>     }

>     if (package %in% .packages()) {
>         name <- paste0("package:", package)
>         previously_attached <- ls(name, all.names = TRUE)
>         if (!all(include.only %in% previously_attached)) {
>             detach(name, character.only = TRUE)
>             include.only <- union(include.only, previously_attached)
>             attachNamespace(package, include.only = include.only)
>         } # else no need to detach and re-attach
>     } else {
>         attachNamespace(package, include.only = include.only)
>     }
>     invisible(TRUE)
> }
> ```

> On Fri, May 23, 2025 at 3:40 AM Roland Fuß via R-devel <
> r-devel@r-project.org> wrote:

>> Hello,
>> 
>> Currently `use` fails silently if you try to attach functions from the
>> same namespace. It would be nice and more similiar to the use of
>> roxygen2 if it could add (and maybe even remove?) functions. Here is a
>> simple proof of concept I have posted on Stack Overflow [1]. Something
>> similar could be done within `use`. Of course, it would be preferable if
>> it didn't need to detach the namespace.
>> 
>> |use("data.table", "fread") ls("package:data.table") #[1] "fread"
>> use("data.table", "nafill") ls("package:data.table") #[1] "fread"
>> useplus <- function(package, include.only) { loaded <-
>> ls(sprintf("package:%s", package), all.names = TRUE)
>> unloadNamespace(package) if (missing(include.only)) { use(package) }
>> else { use(package, union(loaded, include.only)) } }
>> useplus("data.table", "nafill") ls("package:data.table") #[1] "fread"
>> "nafill"|
>> 
>> 
>> [1] https://stackoverflow.com/q/79633064/1412059
>> 
>> --
>> Dr. Roland Fuß
>> 
>> Thünen-Institut für Agrarklimaschutz/
>> Thünen Institute of Climate-Smart Agriculture
>> 
>> Bundesallee 65
>> D-38116 Braunschweig, Germany
>> 
>> Tel./Webex: ++49 531 25701967
>> Email: roland.f...@thuenen.de
>> 
>> Arbeitsgruppe "Emissionsberichterstattung"/
>> Working group "Emission Inventories"
>> Email: emissionsinvent...@thuenen.de
>> 
>> ______________________________________________
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 

>       [[alternative HTML version deleted]]

> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to