r - 包内函数在缺少可见绑定时产生警告
问题描述
在包中的函数内部使用witin
会强制返回警告R CMD check
。
代表
功能:
# Test within
hello <- function(x = data.frame(valA = 1, valB = 2)) {
within(data = x,
expr = {
res = valA + valB
})
}
将产生以下警告R CMD check
:
❯ checking R code for possible problems ... NOTE
hello: no visible binding for global variable ‘valA’
hello: no visible binding for global variable ‘valB’
Undefined global functions or variables:
valA valB
解决方法(解决方案)
添加valA <- valB <- NULL
将使警告静音。
hello <- function(x = data.frame(valA = 1, valB = 2)) {
valA <- valB <- NULL
within(data = x,
expr = {
res = valA + valB
})
}
问题
这种恶作剧valA <- valB <- NULL
对我来说感觉很糟糕。处理这种情况的正确方法是什么?
我想使用within
该功能适合更广泛的功能设计。消除警告的正确方法是什么。我在 GitHub 上遇到了类似的讨论,建议with
在稍微不同的情况下使用。
解决方案
一种解决方案是globalVariables(c('valA', 'valB'))
在包中的某处包含,例如在文件夹中的单独.R
文件中R
。
否则我会这样做:
hello <- function(valA = 1, valB = 2) {
within(data = data.frame(valA, valB),
expr = {
res = valA + valB
})
}
推荐阅读
- xcode - Xcode 内存编辑器
- php - Laravel 中不存在目标类 [App\Http\Controllers\X]
- html - 动画汉堡切换按钮无法正常工作
- node.js - Laravel Mix: npm run dev 和 npm run watch error-> configuration.loader 应该是一个对象
- php - 在视图中使用 baseController 中的对象变量时出现错误
- kubernetes - 没有钩子的 kubernetes preStop 优雅关闭超时
- google-ads-api - 尝试为 Google Adwords (V201809) API 设置代理服务器
- java - 将图像加载到 Java 数据库时出错
- android - Android 崩溃 - kotlin.UninitializedPropertyAccessException MainActivity.getAdapterGallery
- facebook - Facebook Conversions API 警告缺少事件 ID?