问题描述
假设一个R包( myPackage )导入了R包 RCircos 通过description文件和NAMESPACE文件.
Assume an R package (myPackage) that imports the R package RCircos via the DESCRIPTION file and the NAMESPACE file.
$ cat DESCRIPTION
Package: myPackage
Imports: RCircos (>= 1.2.0)
...
$ cat NAMESPACE
import(RCircos)
...
RCircos 的优点之一是它定义了一个自定义环境(称为 RCircos.Env ),并从其各种功能读取/写入该环境的变量.例如,函数 RCircos.Initialize.Plot.Parameters 读写在这种环境下.
One of the perks of RCircos is that it defines a custom environment (called RCircos.Env) and reads/writes variables to this environment from various of its functions. For example, function RCircos.Initialize.Plot.Parameters reads and writes to this environment.
...
RCircosEnvironment <- NULL;
RCircosEnvironment <- get("RCircos.Env", envir = globalenv());
RCircosEnvironment[["RCircos.PlotPar"]] <- plot.param;
(其他R软件包也已经认识到这种特殊行为;例如,参见此包).
(This peculiar behavior has also been recognized by other R packages; see, for example, lines 247-249 of this package).
不幸的是,当我仅通过 RCircos 导入 RCircos 时,似乎无法在 myPackage 中立即识别出环境 RCircos.Env .Description文件和NAMESPACE文件.
Unfortunately, it seems that the environment RCircos.Env is not recognized out of the box in myPackage when I simply import RCircos via the DESCRIPTION file and the NAMESPACE file.
那该怎么办?
似乎有两个选项可以使环境 RCircos.Env 可以访问 RCircos.Initialize.Plot.Parameters
之类的函数.但是,这两个选项都会导致CRAN检查( R CMD检查myPackage --as-cran
)在提交之前对 myPackage 进行强制评估期间发出警告或注释.到CRAN,从而阻止其被CRAN接受.
There seem to be two options of making the environment RCircos.Env accessible to functions like RCircos.Initialize.Plot.Parameters
. However, both of these options cause the CRAN check (R CMD check myPackage --as-cran
) to issue WARNINGs or NOTEs during the mandatory evaluation of myPackage prior to the submission to CRAN, thus preventing its acceptance on CRAN.
选项1 :我在函数要求对象之前添加了以下行:
Option 1: I include the following line directly prior to the function demanding the object:
# my code here #
assign("RCircos.Env", RCircos::RCircos.Env, .GlobalEnv)
RCircos.Set.Core.Components(...)
# my code here #
但是,CRAN检查用NOTE突出显示了这一行,从而阻止在CRAN上接受 myPackage .
However, the CRAN check highlights this line with a NOTE, thus preventing the acceptance of myPackage on CRAN.
* checking R code for possible problems ... NOTE
Found the following assignments to the global environment:
File ‘PACViR/R/visualizeWithRCircos.R’:
assign("RCircos.Env", RCircos::RCircos.Env, .GlobalEnv)
选项2 :在功能需要对象之前,我先加载整个 RCircos 库:
Option 2: I load the entire RCircos library prior to the function demanding the object:
# my code here #
library(RCircos)
RCircos.Set.Core.Components(...)
# my code here #
但是,CRAN检查会在警告中突出显示此选项,再次阻止在CRAN上接受 myPackage .
However, the CRAN check highlights this option with a WARNING, again preventing the acceptance of myPackage on CRAN.
* checking dependencies in R code ... WARNING
'library' or 'require' call not declared from: ‘RCircos’
'library' or 'require' call to ‘RCircos’ in package code.
Please use :: or requireNamespace() instead.
See section 'Suggested packages' in the 'Writing R Extensions' manual.
当然,必须有一种简单且与CRAN兼容的方法,使环境 RCircos.Env 可以通过 RCircos.Set.Core.Components
之类的功能进行访问.em> myPackage !有人可以这样命名和解释吗?
Surely, there must be an easy and CRAN-compatible way of making the environment RCircos.Env accessible to functions such as RCircos.Set.Core.Components
within myPackage! Can someone name and explain such a way?
推荐答案
显然,正常的重新导出不适用于环境,而不适用于功能.但这确实可行:
Apparently the normal re-export does not work with environments as it does with functions. But this does work:
RCircos.Env <- RCircos::RCircos.Env
#' test
#'
#' @param ... data
#'
#' @export
test_fun <- function(...) {
RCircos::RCircos.Set.Core.Components(...)
}
使用 DESCRIPTION
:
Package: test
Type: Package
Title: test
Description: This is a description.
Version: 0.1.0
Authors@R: person("Wouter", "van der Bijl",
email = "[email protected]",
role = c("aut", "cre"))
Maintainer: Wouter van der Bijl <[email protected]>
License: GPL-3
Encoding: UTF-8
LazyData: true
Imports: RCircos
RoxygenNote: 6.1.1
还有这个 NAMESPACE
:
# Generated by roxygen2: do not edit by hand
export(test_fun)
测试:
library(test)
data(UCSC.HG19.Human.CytoBandIdeogram, package = 'RCircos')
test_fun(UCSC.HG19.Human.CytoBandIdeogram)
基本上,当 RCircos
运行 get("RCircos.Env",envir = globalenv())
时,它将遍历搜索路径,直到找到而是从您的软件包中选择> RCircos.Env
.
Basically, when RCircos
runs get("RCircos.Env", envir = globalenv())
, it will traverse up the search path until it finds the RCircos.Env
from your package instead.
运行R CMD Check时,我得到0个错误,0个警告,0个注释.
When running R CMD Check I get 0 errors, 0 warnings, 0 notes.
请注意, RCircos
使用的这种策略以及通过使用 get(..,envir = globalenv())
进行查找的环境实际上是非正统的,通常这不是一个好主意.R函数通常不应有副作用,例如编辑看不见的环境.设置默认值等通常使用 options()
完成.整个程序包可能不是您想要模仿的,但至少现在您可以将其用作依赖项.
Note that this strategy that RCircos
uses, with an environment that gets looked up by using get(.., envir = globalenv())
is really unorthodox and generally not a good idea. R functions should generally not have side-effects, such as editing unseen environments. Setting default values etc. is typically done using options()
. The whole package is probably not something you'd want to emulate, but at least now you can use it as a dependency.
这篇关于R软件包-从导入的软件包转移环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!