r-如何正确记录R6自身



我有一个用R6类构建的函数,想知道传递devtools::check()的最佳方式是

> checking R code for possible problems ... NOTE
obj_gen : <anonymous>: no visible binding for global variable ‘self’
Undefined global functions or variables:
self

然而,它只在实际调用self时给出注释。即在打印函数中,但不在初始化内部的赋值中。

在Tidyverse(此处(中,使用importFrom R6 R6Class。然而,在这种情况下,打印函数中self的调用似乎会触发全局变量注释。

Repex

#' func
#' @param ... opts
#' @examples
#'dontrun{
#' obj_gen(bar = "fubar")
#'}
obj_gen <- function(...){
#' @importFrom R6 R6Class
obj <- R6::R6Class("my_class",
public = list(
foo = NULL,
initialize = function(bar = NA){
self$foo <- bar
},
print = function(){
cat("Anyone for ",
self$foo,
"?",
sep = "")
}
)
)
obj$new(...)
}

一位同事非常乐于助人地建议将其添加到我正在考虑的globalVariables(信息(中。我想知道是否有更好的方法来处理它,使用文档,但是:(

我的Roxygen版本是7.1.1。

具有伪self <- NA定义的解决方案。

#' func
#' @param ... opts
#' @import R6
#' @examples
#'dontrun{
#' obj_gen(bar = "fubar")
#'}
obj_gen <- function(...){
self <- NA
obj <- R6Class("my_class",
public = list(
foo = NULL,
initialize = function(bar = NA) {
self$foo <- bar
},
print = function() {
cat("Anyone for ",
self$foo,
"?",
sep = "")
}
)
)
obj$new(...)
}

最新更新