Fastest way to register namespaces programatically.

huangapple go评论61阅读模式
英文:

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 and flextable are stated (xtable is a dependency of flextable)
  • for the example, after unloading flextable, only xtable 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(&quot;align&quot;)$where
#&gt; character(0)

flextable::as_flextable
#&gt; function (x, ...) 
#&gt; {
#&gt;     UseMethod(&quot;as_flextable&quot;)
#&gt; }
#&gt; &lt;bytecode: 0x000000002158e7f0&gt;
#&gt; &lt;environment: namespace:flextable&gt;

getAnywhere(&quot;align&quot;)$where
#&gt; [1] &quot;namespace:xtable&quot;    &quot;namespace:flextable&quot;

pkgload::unload(&quot;flextable&quot;)
getAnywhere(&quot;align&quot;)$where
#&gt; [1] &quot;namespace:xtable&quot;

<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 and flextable are stated (xtable is a dependency of flextable)
  • for the example, after unloading flextable, only xtable 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(&quot;align&quot;)$where
# character(0)
x &lt;- list(align=function() 1)
attach(x)
getAnywhere(&quot;align&quot;)$where
# [1] &quot;x&quot;
detach(x)
getAnywhere(&quot;align&quot;)$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(&quot;align&quot;)$where
#&gt; character(0)
loadNamespace(&quot;flextable&quot;)
#&gt; &lt;environment: namespace:flextable&gt;
getAnywhere(&quot;align&quot;)$where
#&gt; [1] &quot;namespace:xtable&quot;    &quot;namespace:flextable&quot;

<sup>Created on 2023-06-30 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月29日 23:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定