【发布时间】:2021-08-24 00:24:58
【问题描述】:
我尝试构建一个使用库 tidyverse 的 R 包。
描述文件如下所示:
Package: myFirstPackage
Title: A initial package
Version: 0.0.1.0
Authors@R:
person(given = "Test",
family = "Test",
role = c("aut", "cre"),
email = "first.last@example.com",
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: a description.
Imports: tidyverse
License: GPL-2
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
所以我将tidyverse 添加到Imports 部分。
我的 R 代码如下所示:
myfunction<-function(){
x<-tibble(
id = c(1, 2, 3, 4),
name = c("Louisa", "Jonathan", "Luigi", "Rachel"),
female = c(TRUE, FALSE, FALSE, TRUE)
)
x %>% pull(id)
}
#' MyDemoFunction
#'
#'
#' @import tidyverse
#' @export
say_hello<-function(){
cat("here1")
myfunction()
}
这里,我也有导入。
我正在使用 R-Studio,一旦按下 Check,我就会得到:
> checking R code for possible problems ... NOTE
myfunction: no visible global function definition for 'tibble'
myfunction: no visible global function definition for '%>%'
myfunction: no visible global function definition for 'pull'
myfunction: no visible binding for global variable 'id'
Undefined global functions or variables:
%>% id pull tibble
你找到了项目here的repo。
在that question 中,通过逐个导入函数解决了问题,但我希望完全导入包:我只想使用 tidyverse 的所有函数,而不明确定义每个函数。在问题here 中提到导入必须在我已经拥有的描述中!
【问题讨论】:
-
你可以用全局变量创建一个文件
-
我添加了一个额外的 r 文件,内容如下 if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) 但是,它没有帮助(见github.com/anewruser/r_package_project/blob/master/R/globalR.R)