英文:
How to set global package variables so they can be accessed by examples in R CMD check?
问题
我现在正在开发一个R包,其中包含可以使用的示例图片。我希望设置这样一个机制,将这些图片的路径存储在一个全局变量中,可以通过加载包或者使用类似 myPackage::myVar
的方式进行访问。
目前,我将这些图片放在 inst/media
文件夹中,并在我的 zzz.R
文件的 .onLoad
函数中设置包含它们路径的变量;该函数如下所示:
.onLoad = function(libname, pkgname) {
dog = system.file("media", "dog.png", package="myPackage")
assign("dog", dog, envir=parent.env(environment()))
}
当我使用 devtools::load_all()
加载包时,我可以直接通过 dog
访问这个变量。然而,当我使用 devtools::build()
构建并手动安装时,我只能通过 myPackage:::dog
访问这个变量。
此外,我在使用roxygen生成的man页面中有一个示例,其中包含以下代码:
ret = parsePath(dog)
当我运行 devtools::check()
时,它返回一个错误,警告信息显示 Error : object dog not found
。
有没有办法可以以一种方式添加这个全局变量,使得我可以通过加载包并键入变量名来访问它们,并且在运行R CMD check期间,示例可以正常运行,不会出现错误?
英文:
I am working on an R package right now that includes example images that can be used. I want to set it up so the paths to those images are stored in a global variable that can be accessed either by loading the package or by using something like myPackage::myVar
.
Currently, I have the images in the inst/media
folder and set variables containing their paths in the .onLoad
function in my zzz.R
file; the function looks like this:
.onLoad = function(libname, pkgname) {
dog = system.file("media", "dog.png", package="myPackage")
assign("dog", dog, envir=parent.env(environment()))
}
When I load my package with devtools::load_all()
, I can access the variable with just dog
. However, when I build with devtools::build()
and install manually, I can only access the variable with myPackage:::dog
.
Further, I have an example in my man pages generated with roxygen that uses the following code:
ret = parsePath(dog)
When I run devtools::check()
it returns an error with this warning saying Error : object dog not found
.
Is there any way that I can add this global variable in a way that allows me to access them by loading the package and typing the name, and allows the examples to run without error during R CMD check?
答案1
得分: 0
问题在于,尽管dog
变量被分配到了环境中,但它没有被包导出;解决方法是按照这篇帖子中描述的方法导出变量。
变量首先通过.onLoad
函数分配到环境中:
.onLoad = function(libname, pkgname) {
dog = system.file("media", "dog.png", package=pkgname)
assign("dog", dog, envir=parent.env(environment()))
}
然后,在另一个文件中,该变量被用引号导出,如下所示:
#' @name dog
#' @title The path to an image of a Dog
#' @export
"dog"
我还不得不手动在NAMESPACE文件中添加export(dog)
,然后roxygen才会自动为我完成。这允许通过myPackage::dog
访问该变量。如果不用引号导出变量,该变量只能通过myPackage:::dog
访问,并且其他函数只能使用dog
而无法看到它。这也通过了devtools::check
。
英文:
The issue was that while the dog
variable was being assigned to the environment, it was not being exported by the package; the solution is to export the variable as described in this post.
The variable is first assigned to the environment with the .onLoad
function:
.onLoad = function(libname, pkgname) {
dog = system.file("media", "dog.png", package=pkgname)
assign("dog", dog, envir=parent.env(environment()))
}
Then in a separate file, the variable is exported in quotes like such:
#' @name dog
#' @title The path to an image of a Dog
#' @export
"dog"
I also had to manually add export(dog)
to the NAMESPACE file before roxygen did it automatically for me. This allows the variable to be accessed by myPackage::dog
. Without exporting the variable in quotes, the variable could only be accessed with myPackage:::dog
and would not be seen by other functions using just dog
. This also passes devtools::check
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论