gpt4 book ai didi

r - 在包加载/卸载 : best practise? 上加载/删除用户定义的单元

转载 作者:行者123 更新时间:2023-12-04 02:24:11 25 4
gpt4 key购买 nike

在我正在开发的包中,我需要定义一个新单位:相当于 100 英尺的飞行高度 (FL)。
units包提供了以下可能性:

units::install_conversion_constant("FL", "ft", 100)

为了使包测试( devtools::test() )和包检查( devtools::test() )都适用于我使用此用户定义单元的单元测试, I discovered我需要在包加载阶段注册它。

这是我所做的:

zzz.R (根据 "When you do need side-effects" 部分的新文件):
# register flight levels (FL) as a unit when loading this package
.onLoad <- function(libname, pkgname) {
# install user-define unit for flight level
units::install_conversion_constant("FL", "ft", 100)

invisible()
}

# register flight levels (FL) as a unit when loading this package
.onUnload <- function(libname, pkgname) {
# uninstall user-define unit for flight level
units::remove_symbolic_unit("FL")

invisible()
}

没有做到这一点并将单位注册码放在一些 R/unit-conversion.R 中文件制作 devtools::test()成功但 devtools::check()失败。

上面的解决方案是否是在包中注册(并删除 [也应该这样做?])新单元的正确方法吗?

最佳答案

这几乎绝对是为您的包裹做这件事的地方。我说几乎是因为每条规则都有异常(exception)。阅读下面的部分,了解更多细节和基本 R 手册中的良好实践建议

https://stat.ethz.ch/R-manual/R-devel/library/base/html/ns-hooks.html

Good practice Loading a namespace should where possible be silent, with startup messages given by .onAttach. These messages (and any essential ones from .onLoad) should use packageStartupMessage so they can be silenced where they would be a distraction.

There should be no calls to library nor require in these hooks. The way for a package to load other packages is via the Depends field in the ‘DESCRIPTION’ file: this ensures that the dependence is documented and packages are loaded in the correct order. Loading a namespace should not change the search path, so rather than attach a package, dependence of a namespace on another package should be achieved by (selectively) importing from the other package's namespace.

Uses of library with argument help to display basic information about the package should use format on the computed package information object and pass this to packageStartupMessage.

There should be no calls to installed.packages in startup code: it is potentially very slow and may fail in versions of R before 2.14.2 if package installation is going on in parallel. See its help page for alternatives.

Compiled code should be loaded (e.g., via library.dynam) in .onLoad or a useDynLib directive in the ‘NAMESPACE’ file, and not in .onAttach. Similarly, compiled code should not be unloaded (e.g., via library.dynam.unload) in .Last.lib nor .onDetach, only in .onUnload.

关于r - 在包加载/卸载 : best practise? 上加载/删除用户定义的单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51709488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com