【问题标题】:How to fix `no visible global function definition` when creating a new R-package创建新的 R 包时如何修复“没有可见的全局函数定义”
【发布时间】: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 devtools r-package


【解决方案1】:

我的清洁检查解决方案。您需要 R 4.1.0 才能使用 |> 运算符。

plainSrc.R:

####This File represents a package used for Index Management

### Overall approach of this package ###
# In order to avoid a static hard coding of the index members
# we use a web-based approach based on finanzen.de from there we
# get the index members for which we offer the data.

#' @import dplyr
#'
myfunction<-function(){

  x <- dplyr::tibble(
    id = c(1, 2, 3, 4),
    name = c("Louisa", "Jonathan", "Luigi", "Rachel"),
    female = c(TRUE, FALSE, FALSE, TRUE)
  )
  x |> dplyr::pull(one_of("id"))
}

#' MyDemoFunction
#'
#'
#' @export
#'
say_hello<-function(){
  cat("here1")
  myfunction()
}

并将 DESCRIPTION 中的 Imports 从 tidyverse 更改为 dplyr。

【讨论】:

  • 有效,但为什么 tidyverse 不起作用 - 它应该是一个超级集 dplyr (dplyr.tidyverse.org/#installation)
  • TL;DR tidyverse 是一次制作几个库的简单包装器。这很难理解。 tidyverse 不适合在其他包中使用。这是一个包,它提供了一种简单的方法来一次库多个包,检查 onAttach 函数(在 library(tidyverse) 时调用)-github.com/tidyverse/tidyverse/blob/master/R/zzz.R。功能例如tidyverse 内部的 :: 无法访问 from dplyr。顺便提一句。注意不要在包内的 .R 文件中使用库。有时我们使用if (!require("package")) stop("you need x to proceed")
猜你喜欢
  • 1970-01-01
  • 2015-03-17
  • 2020-01-21
  • 2020-11-18
  • 2015-11-11
  • 2015-09-16
  • 2015-08-12
  • 1970-01-01
相关资源
最近更新 更多