英文:
Fastest way to register namespaces programatically
问题
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:
- at first, no loaded namespace contain a function named "align"
- after printing any function from
flextable
,xtable
andflextable
are stated (xtable
is a dependency offlextable
) - for the example, after unloading
flextable
, onlyxtable
remains
This 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?
英文:
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"
<sup>Created on 2023-06-29 with reprex v2.0.2</sup>
As you can see:
- at first, no loaded namespace contain a function named "align"
- after printing any function from
flextable
,xtable
andflextable
are stated (xtable
is a dependency offlextable
) - for the example, after unloading
flextable
, onlyxtable
remains
This 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?
答案1
得分: 0
如果您不需要使用现有包中的函数,可以使用attach()/detach()
快速地将东西放入和移出搜索路径。
getAnywhere("align")$where
# character(0)
x <- list(align=function() 1)
attach(x)
getAnywhere("align")$where
# [1] "x"
detach(x)
getAnywhere("align")$where
# character(0)
英文:
If you don't need to use functions from existing packages, you can use use attach()/detach()
to quickly put things on and off the search path
getAnywhere("align")$where
# character(0)
x <- list(align=function() 1)
attach(x)
getAnywhere("align")$where
# [1] "x"
detach(x)
getAnywhere("align")$where
# character(0)
答案2
得分: 0
实际上,有一个明显命名为 base::loadNamespace()
的快速函数。不知道我是怎么错过它的...
getAnywhere("align")$where
#> character(0)
loadNamespace("flextable")
#> <environment: namespace:flextable>
getAnywhere("align")$where
#> [1] "namespace:xtable" "namespace:flextable"
创建于2023-06-30,使用 reprex v2.0.2
英文:
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"
<sup>Created on 2023-06-30 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论