I'm building a function that uses getAnywhere()
to retrieve the different packages from which a given function can be exported.
Unfortunately, getAnywhere()
is limited to functions that exist in a loaded namespace, so I need to register the one I use.
Time is a constraint here, as loading a lot of namespaces can be a bit long (>10s), so using library()
or require()
is not realistic.
In RStudio, simply printing a prefixed function is enough to register the whole namespace without loading the library:
getAnywhere("align")$where
#> character(0)
flextable::as_flextable
#> function (x, ...)
#> {
#> UseMethod("as_flextable")
#> }
#> <bytecode: 0x000000002158e7f0>
#> <environment: namespace:flextable>
getAnywhere("align")$where
#> [1] "namespace:xtable" "namespace:flextable"
pkgload::unload("flextable")
getAnywhere("align")$where
#> [1] "namespace:xtable"
Created on 2023-06-29 with reprex v2.0.2
As you can see:
flextable
, xtable
and flextable
are stated (xtable
is a dependency of flextable
)flextable
, only xtable
remainsThis seems fast enough, but it is hard-coded.
How could I register the flextable
namespace programmatically (along with others)?
Is there an even faster way to do so?
Actually, there is a fast little function obviously named base::loadNamespace()
. Don't know how I missed it...
getAnywhere("align")$where
#> character(0)
loadNamespace("flextable")
#> <environment: namespace:flextable>
getAnywhere("align")$where
#> [1] "namespace:xtable" "namespace:flextable"
Created on 2023-06-30 with reprex v2.0.2