我在 R 中有以下 2 个函数:
exs.time.start<-function(){
exs.time<<-proc.time()[3]
return(invisible(NULL))
}
exs.time.stop<-function(restartTimer=TRUE){
if(exists('exs.time')==FALSE){
stop("ERROR: exs.time was not found! Start timer with ex.time.start")
}
returnValue=proc.time()[3]-exs.time
if(restartTimer==TRUE){
exs.time<<-proc.time()[3]
}
message(paste0("INFO: Elapsed time ",returnValue, " seconds!"))
return(invisible(returnValue))
}
该函数使用我调用该函数时的 CPU 时间exs.time.start
创建一个全局变量 ( )。exs.time
该函数exs.time.stop
访问该全局变量并返回执行exs.time.start
和之间的时间exs.time.stop
。
我的目标是用这两个函数在 R 中创建一个包。如何将该全局变量 ( exs.time
) 定义为对用户不可见的变量,因此他在 R 全局环境中看不到该变量?
我可以将此变量定义为 R 包环境/命名空间中的“隐藏”全局变量吗?
这是我第一次使用包,所以我不知道在定义包时如何很好地使用命名空间文件。我正在使用 R Studio 和 Roxygen2 创建我的包。
任何帮助或建议都会很棒!