>>>>> 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 at 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.fuss at thuenen.de >> >> Arbeitsgruppe "Emissionsberichterstattung"/ >> Working group "Emission Inventories" >> Email: emissionsinventare at thuenen.de >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >>> [[alternative HTML version deleted]]> ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
On Tuesday, May 27th, 2025 at 9:13 AM, Kurt Hornik <Kurt.Hornik at wu.ac.at> wrote:> > > > > > 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 :-)Some work has been done on that front during last week's r-dev-day with Ella Kaye and Philippe Grosjean [1]. After some more thinking, I think we haven't been stringent enough, and would suggest that the user should be alerted as soon as what they want to attach doesn't match exactly what is already on the search path. Something along the lines of (the warning/message can of course be amended). These two situations are OK, given that exactly the same set is asked for repeated, either with a limited set of functions:> library("Matrix", include.only = "isDiagonal") > library("Matrix", include.only = "isDiagonal") ## okor, in a new session, with the whole package:> library("Matrix", include.only = "isDiagonal") > library("Matrix", include.only = "isDiagonal") ## okBelow, (again, in a new session) the user could be mislead that either only qr would be attached (and isDiagonal not anymore), or all Matrix's exported symbols:> library("Matrix", include.only = "isDiagonal") > library("Matrix", include.only = "qr") ## warnWarning message: In library("Matrix", include.only = "qr") : The package 'Matrix' is already loaded with a different set of functions. Please detach() it first.> library("Matrix") ## warnWarning message: In library("Matrix") : The package 'Matrix' is already loaded with a different set of functions. Please detach() it first. In the following cases, a subset is asked after all symbols are attached, which also triggers a warning/message:> library("Matrix") > library("Matrix", exclude = "isDiagonal") ## warnWarning message: In library("Matrix", exclude = "isDiagonal") : The package 'Matrix' is already loaded with a different set of functions. Please detach() it first.> library("Matrix", include.only = "isDiagonal") ## warnWarning message: In library("Matrix", include.only = "isDiagonal") : The package 'Matrix' is already loaded with a different set of functions. Please detach() it first. If this is a good way forward, I would be happy to carry on that work. Laurent [1] https://github.com/r-devel/r-dev-day/issues/48