R包未能通过devtools :: check,因为“找不到函数”即使该功能已导入NAMESPACE [英] R package fails devtools::check, because "could not find function" even though the function is imported in NAMESPACE
问题描述
尝试使用 roxygen2
和 devtools $ c构建我的第一个
R
软件包$ c>。我在 @examples <中添加了使用
%>%
和 mutate
的函数/ code>部分。当我运行 check()
时,它失败了,因为它找不到函数%>%
或变异
。
Trying to build my first R
package using roxygen2
and devtools
. I have added a function that uses %>%
and mutate
in the @examples
section. When I run check()
it fails, because it cannot find the function %>%
or mutate
.
Based on this, this, and this I have tried the following:
我有#'importFrom magrittr%>%
和#'importFrom dplyr mutate
在函数的 .R
文件中。我还在 Imports:
下的 magrittr
和 dplyr
DESCRIPTION
文件。运行 document()
之后,我的 NAMESPACE
文件包含 importFrom(dplyr,mutate)
和 importFrom(magrittr,%>%)
。
I have #' importFrom magrittr %>%
and #' importFrom dplyr mutate
in the function's .R
file. I also have magrittr
and dplyr
under Imports:
in the DESCRIPTION
file. After running document()
, my NAMESPACE
file contains importFrom(dplyr,mutate)
and importFrom(magrittr,"%>%")
.
最小 R / test.R
文件:
#' Conditional mutate
#'
#' \code{mutate_cond} mutates the \code{data.frame} only on the rows that
#' satisfy the condition.
#'
#' @param .data \code{data.frame}
#' @param condition expression with the condition to be evaluated
#' @param ... arguments passed to \code{mutate}
#' @param envir environment inherited from \code{parent.frame()}
#'
#' @return \code{data.frame}
#' @importFrom dplyr mutate
#' @importFrom magrittr %>%
#'
#' @examples
#' data(iris)
#' iris %>%
#' mutate(aux = 0) %>%
#' mutate_cond(Petal.Length > 1.3,aux = 3)
#'
#' @export
mutate_cond <- function(.data, condition, ..., envir = parent.frame()) {
condition <- eval(substitute(condition), .data, envir)
.data[condition, ] <- .data[condition, ] %>% mutate(...)
.data
}
最小 DESCRIPTION
文件:
Package: test
Version: 0.1
Date: 2019-06-07
Title: Functions
Description: Some functions I use.
Author: me
Maintainer: me <myemail@email.com>
Encoding: UTF-8
License: GPL-3
Imports: dplyr, magrittr
NAMESPACE
用 document()
生成:
# Generated by roxygen2: do not edit by hand
export(mutate_cond)
importFrom(dplyr,mutate)
importFrom(magrittr,"%>%")
我希望此示例代码能够成功运行并传递 check()
。相反,我收到此错误消息:
I expect this example code to run successfully and pass check()
. Instead I get this error message:
❯ checking examples ... ERROR
Running examples in ‘test-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: mutate_cond
> ### Title: Conditional mutate
> ### Aliases: mutate_cond
>
> ### ** Examples
>
> data(iris)
> iris %>%
+ mutate(aux = 0) %>%
+ mutate_cond(Petal.Length > 1.3,aux = 3)
Error in iris %>% mutate(aux = 0) %>% mutate_cond(Petal.Length > 1.3, :
could not find function "%>%"
Execution halted
1 error ✖ | 0 warnings ✔ | 0 notes ✔
此外,如果我添加 require(dplyr)
和 require(magrittr)
到 @examples
部分,错误消失了,或者如果我删除了整个 @examples
部分,错误消失了。
Also, if I add require(dplyr)
and require(magrittr)
to the @examples
section the error goes away or if I remove the whole @examples
section the error goes away.
为什么不这样做打包通过 check()
?
Why won't this package pass check()
?
谢谢!
推荐答案
在控制台中运行 usethis :: use_pipe()
也可以解决问题。
Running usethis::use_pipe()
in the console will do the trick as well.
这篇关于R包未能通过devtools :: check,因为“找不到函数”即使该功能已导入NAMESPACE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!