驯服 R 中函数调用的导出
Taming exports of function call in R
我正在打电话给
goseq::goseq。
在那之后,命名空间(如果这是正确的词)都被打乱了。
我想这可以追溯到在包或其依赖项中自由使用 library(...)
。
如何防止调用屏蔽我的命名空间中的任何对象?
...
Attaching package: ‘BiocGenerics’
The following objects are masked from ‘package:parallel’:
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ, clusterExport,
clusterMap, parApply, parCapply, parLapply, parLapplyLB, parRapply, parSapply,
parSapplyLB
The following objects are masked from ‘package:dplyr’:
combine, intersect, setdiff, union
[... ETC, ETC ...]
Attaching package: ‘AnnotationDbi’
The following object is masked from ‘package:dplyr’:
select
P.S.
如果有人可以用适当的技术术语来表述问题,我会考虑作为答案。例如,为什么 rlang::env_names(rlang::current_env())
只显示我设置的变量,而不是浮动的对象,我在哪里可以找到它们?由于误报,我找不到好的建议。
作为概念验证,这似乎可行。
感谢@KonradRudolph 的评论。
# install.packages("crunch")
# Wrap functions that do `library(...)`
# and force-detach all attached packages
unattach_packages <- function(f) {
requireNamespace("crunch")
# wrapper for f
function(...) {
base::with(
crunch::ContextManager(
enter = base::search,
exit = function() {
for (pkg in setdiff(base::search(), enter)) {
base::detach(pkg, character.only = TRUE, force = TRUE)
}
},
# this overwrites the variable `enter`, if existent
as = "enter"
),
{
f(...)
}
)
}
}
# Usage:
unattach_packages(function(x, y) { library(goseq); x + y })(1, 2)
我正在打电话给 goseq::goseq。 在那之后,命名空间(如果这是正确的词)都被打乱了。
我想这可以追溯到在包或其依赖项中自由使用 library(...)
。
如何防止调用屏蔽我的命名空间中的任何对象?
...
Attaching package: ‘BiocGenerics’
The following objects are masked from ‘package:parallel’:
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ, clusterExport,
clusterMap, parApply, parCapply, parLapply, parLapplyLB, parRapply, parSapply,
parSapplyLB
The following objects are masked from ‘package:dplyr’:
combine, intersect, setdiff, union
[... ETC, ETC ...]
Attaching package: ‘AnnotationDbi’
The following object is masked from ‘package:dplyr’:
select
P.S.
如果有人可以用适当的技术术语来表述问题,我会考虑作为答案。例如,为什么 rlang::env_names(rlang::current_env())
只显示我设置的变量,而不是浮动的对象,我在哪里可以找到它们?由于误报,我找不到好的建议。
作为概念验证,这似乎可行。 感谢@KonradRudolph 的评论。
# install.packages("crunch")
# Wrap functions that do `library(...)`
# and force-detach all attached packages
unattach_packages <- function(f) {
requireNamespace("crunch")
# wrapper for f
function(...) {
base::with(
crunch::ContextManager(
enter = base::search,
exit = function() {
for (pkg in setdiff(base::search(), enter)) {
base::detach(pkg, character.only = TRUE, force = TRUE)
}
},
# this overwrites the variable `enter`, if existent
as = "enter"
),
{
f(...)
}
)
}
}
# Usage:
unattach_packages(function(x, y) { library(goseq); x + y })(1, 2)