什么是“@export”R 文档字符串

What is the '@export' R docstring

我正在编写一个 R 包,我注意到许多其他包在它们的某些函数上使用了 @export 文档字符串,我正在尝试找到一个规范定义(我假设它将函数标记为可用于导出)。来自文档字符串包 documentation,

@export, @import, @importFrom These are keywords that are highly useful for and only really make sense in the context of package creation. docstring isn’t meant to be used for full packages so you wouldn’t expect me to comment on these keywords. However, docstring is a nice tool to use in the time before package creation so it is understandable that if somebody thought their code will turn into a package someday that they might want to get a jump start on all of the documentation (including the namespace directives). There is nothing stopping a user from using these keywords. They won’t do anything when used with docstring though.

这让我感到困惑,因为我确实看到使用@export的包;不管它是否在包中使用,我都无法从这个定义中提取任何有用的信息。

@export 文档字符串的定义是什么,应该在什么时候使用?

@export docstring 通知 Roxygen 将函数名称放在包的 NAMESPACE 文件中,这意味着用户可以在 运行 library(<packagename>) 之后访问它。它应该用于您希望包的消费者能够直接使用的功能。您可能拥有不一定要公开的私有辅助函数。

此处摘自 R 包一书中的一些摘录,可能有助于理解。

For a function to be usable outside of your package, you must export it. You export functions that you want other people to use. Generally, it’s better to export too little than too much. It’s easy to export things that you didn’t before; it’s hard to stop exporting a function because it might break existing code. Always err on the side of caution, and simplicity. It’s easier to give people more functionality than it is to take away stuff they’re used to.

您可以在此处阅读本书的导出部分 https://r-pkgs.org/namespace.html#exports