[英]R: how to properly include libraries in my package
I'm writing by first R-Package and was wondering what the right way is to include other libraries, like ggplot2
.我正在编写第一个 R-Package 并且想知道包含其他库的正确方法是什么,例如
ggplot2
。 So there are two places where an import statement can go in my package.所以在我的package中有两个地方可以导入语句go。 The
DESCRIPTION
file and the NAMESPACE
, where the latter requires to include ggplot in some roxagen statement like #'@import ggplot2
. DESCRIPTION
文件和NAMESPACE
,后者需要在一些 roxagen 语句中包含 ggplot ,例如#'@import ggplot2
。
Actually I thought that is is enough to include ggplot2 inside DESCRIPTION, as I thought that loading my packages will also load or dependencies, however, when ggplot2 is not in the namespace it seems like I cannot use function, eg aes, ggplot
without writing ggplot2::aes or ggplot2::gpplot
. Actually I thought that is is enough to include ggplot2 inside DESCRIPTION, as I thought that loading my packages will also load or dependencies, however, when ggplot2 is not in the namespace it seems like I cannot use function, eg
aes, ggplot
without writing ggplot2::aes or ggplot2::gpplot
。 So for what are the import statements in each of the config files, ie DESCRIPTION and NAMESPACE?那么每个配置文件中的导入语句是什么,即DESCRIPTION和NAMESPACE?
The one required step is to include ggplot2
under Imports:
in your DESCRIPTION
file.所需的一个步骤是在您的
DESCRIPTION
文件中的Imports:
下包含ggplot2
。 This ensures that ggplot2 is installed when your package is installed.这可确保在安装 package 时安装 ggplot2。
Then, to use functions from ggplot2 in your package, you need to tell R where to look for them.然后,要在 package 中使用 ggplot2 中的函数,您需要告诉 R 在哪里寻找它们。 You can do this in 3 ways:
您可以通过 3 种方式做到这一点:
ggplot2::aes()
.ggplot2::aes()
。 This is my preferred method, as it doesn't add anything extra to your NAMESPACE
, avoids any name collisions, and avoids breakage if you later rework or remove a function.NAMESPACE
中添加任何额外内容,避免任何名称冲突,并且在您以后返工或移除 function 时避免损坏。#' @importFrom ggplot2 aes
in the roxygen block above the function using it.#' @importFrom ggplot2 aes
放在使用它的 function 上方的 roxygen 块中来做到这一点。 Add however many function names you need to that line.aes()
directly without having to specify the namespace it's coming from with ggplot2::aes()
.aes()
而无需使用ggplot2::aes()
指定它来自的命名空间。 This can be convenient if you're working with a lot of functions (like with ggplot2), but I recommend using the first option instead in general to keep your NAMESPACE tidier.#' @import ggplot2
in a roxygen block.#' @import ggplot2
放在 roxygen 块中来导入整个 package 函数。 This imports every function from a package.ggplot2::aes()
.ggplot2::aes()
调用它们:If you depend on a package you should put it in the Imports
field of the DESCRIPTION file, after which you can use pkgname::function()
in your code.如果您依赖 package,您应该将其放在说明文件的
Imports
字段中,之后您可以在代码中使用pkgname::function()
。 usethis::use_package()
function can help you do this. usethis::use_package()
function 可以帮助您做到这一点。
If you want your code to be able to use any code of the package without the use of ::
, you should put a roxygen comment somewhere like this:如果您希望您的代码能够在不使用
::
的情况下使用 package 的任何代码,您应该在以下位置添加 roxygen 注释:
#' @import pkgname
NULL
This then gets ported by roxygen2 to your NAMESPACE file.然后由 roxygen2 将其移植到您的 NAMESPACE 文件中。
If you want to specifically use some functions (but not others), you can use the following that is used by roxygen2:如果您想专门使用某些功能(但不是其他功能),您可以使用 roxygen2 使用的以下功能:
#' @importFrom pkgname fun1 fun2
NULL
The usethis::use_import_from()
function can help you do the above. usethis::use_import_from()
function 可以帮助您完成上述工作。 In the examples above NULL
only indicates that you're not documentating a function or data, and you can use it at the end of a documentation comment block.在上面的示例中,
NULL
仅表示您没有记录 function 或数据,您可以在文档注释块的末尾使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.