简体   繁体   English

R:如何在我的 package 中正确包含库

[英]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 种方式做到这一点:

  1. Put the name of the package before the function name when you use it: ggplot2::aes() .使用时将 package 的名称放在 function 名称之前: 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 时避免损坏。
  2. Import specific functions as you use them.使用时导入特定功能。 Do this by putting #' @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.将您需要的许多 function 名称添加到该行。 After doing that, you can use 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.如果您使用很多函数(例如使用 ggplot2),这可能会很方便,但我建议通常使用第一个选项来保持 NAMESPACE 更整洁。 You technically only need to include the @importFrom line for once in your package for any function, but I recommend including it for each function that is using the imported functions.对于任何 function,从技术上讲,您只需要在 package 中包含一次 @importFrom 行,但我建议将其包含在使用导入函数的每个 function 中。 That way, if you ever rework or remove a function, the other functions don't break.这样,如果您返工或移除 function,其他功能不会中断。
  3. You can import a whole package of functions by putting #' @import ggplot2 in a roxygen block.您可以通过将#' @import ggplot2放在 roxygen 块中来导入整个 package 函数。 This imports every function from a package.这会从 package 导入每个 function。 This can be a lot for big packages like ggplot2 and dplyr, and the can potentially cause issues, so I suggest never doing this and instead importing only the specific functions you need or calling them with ggplot2::aes() .这对于像 ggplot2 和 dplyr 这样的大包来说可能很多,并且可能会导致问题,所以我建议永远不要这样做,而只导入您需要的特定函数或使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM