Fastest way to register namespaces programatically.

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

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:

  1. getAnywhere("align")$where
  2. #> character(0)
  3. flextable::as_flextable
  4. #> function (x, ...)
  5. #> {
  6. #> UseMethod("as_flextable")
  7. #> }
  8. #> <bytecode: 0x000000002158e7f0>
  9. #> <environment: namespace:flextable>
  10. getAnywhere("align")$where
  11. #> [1] "namespace:xtable" "namespace:flextable"
  12. pkgload::unload("flextable")
  13. getAnywhere("align")$where
  14. #> [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:

  1. getAnywhere(&quot;align&quot;)$where
  2. #&gt; character(0)
  3. flextable::as_flextable
  4. #&gt; function (x, ...)
  5. #&gt; {
  6. #&gt; UseMethod(&quot;as_flextable&quot;)
  7. #&gt; }
  8. #&gt; &lt;bytecode: 0x000000002158e7f0&gt;
  9. #&gt; &lt;environment: namespace:flextable&gt;
  10. getAnywhere(&quot;align&quot;)$where
  11. #&gt; [1] &quot;namespace:xtable&quot; &quot;namespace:flextable&quot;
  12. pkgload::unload(&quot;flextable&quot;)
  13. getAnywhere(&quot;align&quot;)$where
  14. #&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()快速地将东西放入和移出搜索路径。

  1. getAnywhere("align")$where
  2. # character(0)
  3. x <- list(align=function() 1)
  4. attach(x)
  5. getAnywhere("align")$where
  6. # [1] "x"
  7. detach(x)
  8. getAnywhere("align")$where
  9. # 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

  1. getAnywhere(&quot;align&quot;)$where
  2. # character(0)
  3. x &lt;- list(align=function() 1)
  4. attach(x)
  5. getAnywhere(&quot;align&quot;)$where
  6. # [1] &quot;x&quot;
  7. detach(x)
  8. getAnywhere(&quot;align&quot;)$where
  9. # character(0)

答案2

得分: 0

实际上,有一个明显命名为 base::loadNamespace() 的快速函数。不知道我是怎么错过它的...

  1. getAnywhere("align")$where
  2. #> character(0)
  3. loadNamespace("flextable")
  4. #> <environment: namespace:flextable>
  5. getAnywhere("align")$where
  6. #> [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...

  1. getAnywhere(&quot;align&quot;)$where
  2. #&gt; character(0)
  3. loadNamespace(&quot;flextable&quot;)
  4. #&gt; &lt;environment: namespace:flextable&gt;
  5. getAnywhere(&quot;align&quot;)$where
  6. #&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:

确定