简体   繁体   English

如何自动将函数加载到 R 包的命名空间中

[英]How to automatically load functions into namespace of an R package

I have an R package that requires functions from several other packages to be in the namespace.我有一个 R 包,它需要来自其他几个包的函数在命名空间中。 Using roxygen2 documentation, I've successfully installed these packages (ie install.packages(dplyr) ), but I am unable to load them in automatically (ie library(dplyr) ).使用 roxygen2 文档,我已经成功安装了这些包(即install.packages(dplyr) ),但我无法自动加载它们(即library(dplyr) )。

Here is my DESCRIPTION file:这是我的描述文件:

Package: pkgname
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "email@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.5.2)
Imports:
  ggplot2,
  zoo,
  tidyr,
  dplyr,
  magrittr
Suggests:
  RColorBrewer
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1

At the end of the documentation for one of my functions that requires the pipe function from magrittr, I included the following notation:在需要来自 magrittr 的管道函数的我的一个函数的文档末尾,我包含了以下符号:

#' @importFrom magrittr %>%
#'
#' @export
funName <- function(...) { 
... 
} 

And the other function:另一个功能:

#' @import ggplot2
#' @importFrom magrittr %>%
#'
#' @export
funName2 <- function(...) {
...
}

And this successfully shows up in my NAMESPACE file:这成功地显示在我的 NAMESPACE 文件中:

# Generated by roxygen2: do not edit by hand

export(funName2)
export(funName)
import(ggplot2)
importFrom(magrittr,"%>%")

However, despite the added notation in roxygen2 comments and the correct script in my NAMESPACE file, I still have to load the packages ggplot2 and magrittr using library(package-name) at every new R session.然而,尽管在 roxygen2 注释中添加了符号并且我的 NAMESPACE 文件中的脚本正确,我仍然必须在每个新的 R 会话中使用library(package-name)加载包ggplot2magrittr I expect this requirement for dplyr , tidyr and zoo (since I do not explicitly load these, just install them), but I did not for ggplot2 or the %>% operator.我希望dplyrtidyrzoo这个要求(因为我没有明确加载这些,只需安装它们),但我没有ggplot2%>%运算符。 Am I importing them incorrectly?我是否错误地导入了它们?

You should never use library(package.name) inside your package functions.你永远不应该在你的包函数中使用library(package.name) Instead use package.name::function.name() .而是使用package.name::function.name() You need to re-export the magrittr pipe operator:您需要重新导出magrittr管道操作符:

1- put magrittr into the DESCRIPTION file (as you did) 1- 将magrittr放入描述文件(如您所做的那样)

2- make an __imports.R file to the packages R/ directory with the following lines: 2- 使用以下__imports.R文件创建到包R/目录中:

#' re-export magrittr pipe operator
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
NULL

Or, similarly do as Hadley Wickham says :或者,就像哈德利·威克姆所说的那样

#' Pipe operator
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL

I think you can do the same for other imports when necessary.我认为你可以在必要时对其他进口做同样的事情。 Use @importFrom as much as you can, otherwise re-export it.尽可能多地使用@importFrom ,否则重新导出它。

Try and run the following command to get your packages into NAMESPACE尝试运行以下命令将您的包放入 NAMESPACE

devtools::document()

After running it, all the imported (@import) packages are gonna be inserted into NAMESPACE.运行后,所有导入的 (@import) 包都将插入到 NAMESPACE 中。

Hope it helps ;)希望能帮助到你 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自动加载R包中的数据? - How to automatically load data in an R package? 如何在R中自动将所有功能加载到项目中 - How to load all my functions in my project automatically in R 从 GitHub 加载 R package 时出现“包或命名空间加载失败”错误 - "package or namespace load failed" error in loading R package from GitHub &#39;sf&#39;(R包)的命名空间加载失败,无法加载共享对象 - namespace load failed for ‘sf’ (R package) , unable to load shared object 如何加载一个包,以便需要使用命名空间引用符号? - How to load a package such that referring to symbols with namespace is needed? 如何在包dev中打开的项目上自动加载和附加导入的函数? - How can I automatically load and attach imported functions on project open in package dev? PowerBI Desktop 和 R 错误:“ggplot2”的包或命名空间加载失败 - PowerBI Desktop and R Error: package or namespace load failed for 'ggplot2' 运行 R Markdown '包或命名空间加载失败'时出现 tidyverse 错误 - tidyverse error while running R Markdown 'package or namespace load failed' r编程-loadnamespace中的错误-包或名称空间加载失败 - r programming - error in loadnamespace - package or namespace load failed 由于名称空间错误,R无法加载包预测 - R cannot load package forecast due to namespace error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM