如何在 R 包中定义 "hidden global variables"?
How to define "hidden global variables" inside R packages?
我在 R 中有以下两个函数:
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))
}
函数 exs.time.start
创建了一个全局变量 (exs.time
),其中包含我调用该函数时的 CPU 时间。
函数 exs.time.stop
访问该全局变量和 return 执行 exs.time.start
和 exs.time.stop
之间的时间。
我的objective是用这两个函数在R中创建一个包。如何将全局变量 (exs.time
) 定义为用户不可见的变量,以便他在 R 全局环境中看不到该变量?
我可以将此变量定义为 R 包 environment/namespace 中的 "hidden" 全局变量吗?
我是第一次使用包,所以我不知道在定义包时如何很好地使用命名空间文件。我正在使用 R Studio 和 Roxygen2 创建我的包。
任何帮助或建议都会很棒!
我在几个包中使用包全局环境:
RcppGSL 存储 config info about the GSL libraries
RPushbullet 存储一些 user-related meta data
可能还有更多,但你明白了。
感谢您分享您的包裹@Dirk Eddelbuettel
我的问题的解决方案如下:
.pkgglobalenv <- new.env(parent=emptyenv())
exs.time.start<-function(){
assign("exs.time", proc.time()[3], envir=.pkgglobalenv)
return(invisible(NULL))
}
exs.time.stop<-function(restartTimer=TRUE){
if(exists('exs.time',envir=.pkgglobalenv)==FALSE){
stop("ERROR: exs.time was not found! Start timer with exs.time.start")
}
returnValue=proc.time()[3]-.pkgglobalenv$exs.time
if(restartTimer==TRUE){
assign("exs.time", proc.time()[3], envir=.pkgglobalenv)
}
message(paste0("INFO: Elapsed time ",returnValue, " seconds!"))
return(invisible(returnValue))
}
- 我在我的 R 文件中创建了一个环境
new.env()
,在我的函数定义之前。
- 我已经使用
assign()
访问环境并更改了我的全局变量的值!
变量被隐藏,一切正常!谢谢大家!
我在 R 中有以下两个函数:
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))
}
函数 exs.time.start
创建了一个全局变量 (exs.time
),其中包含我调用该函数时的 CPU 时间。
函数 exs.time.stop
访问该全局变量和 return 执行 exs.time.start
和 exs.time.stop
之间的时间。
我的objective是用这两个函数在R中创建一个包。如何将全局变量 (exs.time
) 定义为用户不可见的变量,以便他在 R 全局环境中看不到该变量?
我可以将此变量定义为 R 包 environment/namespace 中的 "hidden" 全局变量吗?
我是第一次使用包,所以我不知道在定义包时如何很好地使用命名空间文件。我正在使用 R Studio 和 Roxygen2 创建我的包。
任何帮助或建议都会很棒!
我在几个包中使用包全局环境:
RcppGSL 存储 config info about the GSL libraries
RPushbullet 存储一些 user-related meta data
可能还有更多,但你明白了。
感谢您分享您的包裹@Dirk Eddelbuettel
我的问题的解决方案如下:
.pkgglobalenv <- new.env(parent=emptyenv())
exs.time.start<-function(){
assign("exs.time", proc.time()[3], envir=.pkgglobalenv)
return(invisible(NULL))
}
exs.time.stop<-function(restartTimer=TRUE){
if(exists('exs.time',envir=.pkgglobalenv)==FALSE){
stop("ERROR: exs.time was not found! Start timer with exs.time.start")
}
returnValue=proc.time()[3]-.pkgglobalenv$exs.time
if(restartTimer==TRUE){
assign("exs.time", proc.time()[3], envir=.pkgglobalenv)
}
message(paste0("INFO: Elapsed time ",returnValue, " seconds!"))
return(invisible(returnValue))
}
- 我在我的 R 文件中创建了一个环境
new.env()
,在我的函数定义之前。 - 我已经使用
assign()
访问环境并更改了我的全局变量的值!
变量被隐藏,一切正常!谢谢大家!