雖然有可能將其分配給全球環境中做到這一點,下面是整潔在我看來,因爲默認的行爲可以很容易地通過detach
恢復荷蘭國際集團的環境:因爲我
首先是一些演示功能不知道你想用哪個包:
packageDemo <- new.env()
makeFun <- function(x) {x;function(param) c(x, param)}
for (i in paste0("f", letters)) assign(i, makeFun(i), packageDemo)
現在這裏是我的咖喱包功能。它將環境或包名稱作爲字符向量(例如"package:myPackage"
)。環境中的所有功能都會被粘貼並添加到返回的新環境中。如果需要的話,這可以是attach
。
curryPackage <- function(package, ..., functionNames = NULL){
if (is.character(package) && !grepl("^package:", package)) {
package <- paste0("package:", package)
}
fnNames <- ls(package)
if (!is.null(functionNames)) {
fnNames <- fnNames[fnNames %in% functionNames]
}
fnNames <- fnNames[vapply(fnNames, function(x) is.function(get(x, package)), logical(1))]
fnList <- lapply(fnNames, function(x){
functional::Curry(get(x, package), ...)
})
names(fnList) <- fnNames
list2env(fnList)
}
現在一些演示
attach(packageDemo)
fc("Testing")
#[1] "fc" "Testing"
fc()
#Error in fc() : argument "param" is missing, with no default
newPack <- curryPackage(packageDemo, param = "demo")
attach(newPack)
#The following objects are masked from packageDemo:
#
# fa, fb, fc, fd, fe, ff, fg, fh, fi, fj, fk, fl, fm, fn, fo, fp, fq, fr, fs, ft, fu, fv, fw, fx, fy, fz
fc("Testing")
#Error in (function (param) : unused argument ("Testing")
fc()
#[1] "fc" "demo"