R:从库中收集所有函数定义
R: Collect All Function Definitions from a Library
我正在使用 R。我在 Whosebug 上找到了之前的 post,它显示了如何获取属于给定库的所有函数的“列表”:
How to find all functions in an R package?
例如:
#load desired library
library(ParBayesianOptimization)
#find out all functions from this library
getNamespaceExports("ParBayesianOptimization")
[1] "addIterations" "getLocalOptimums" "bayesOpt" "getBestPars" "changeSaveFile" "updateGP"
上面的代码告诉我在“ParBayesianOptimization”库中使用的所有函数的名称。从这里,我可以手动检查这些功能中的每一个 - 例如:
# manually inspect any one of these functions
getAnywhere(bayesOpt)
A single object matching ‘bayesOpt’ was found
It was found in the following places
package:ParBayesianOptimization
namespace:ParBayesianOptimization
with value
#function stats here
function (FUN, bounds, saveFile = NULL, initGrid, initPoints = 4,
iters.n = 3, iters.k = 1, otherHalting = list(timeLimit = Inf,
minUtility = 0), acq = "ucb", kappa = 2.576, eps = 0,
parallel = FALSE, gsPoints = pmax(100, length(bounds)^3),
convThresh = 1e+08, acqThresh = 1, errorHandling = "stop",
plotProgress = FALSE, verbose = 1, ...)
{
startT <- Sys.time()
optObj <- list()
etc etc etc ...
saveFile = saveFile, verbose = verbose, ...)
return(optObj)
}
#function ends here
<bytecode: 0x000001cbb4145db0>
<environment: namespace:ParBayesianOptimization>
目标 : 是否可以使用这些函数中的每一个并创建一个包含它们完整定义的记事本文件?
看起来像这样的东西:
我的尝试:
我想我可以先在 R 中创建一个“对象”,其中包含在该库中找到的所有函数:
library(plyr)
a = getNamespaceExports("ParBayesianOptimization")
my_list = do.call("rbind.fill", lapply(a, as.data.frame))
X[[i]]
1 addIterations
2 getLocalOptimums
3 bayesOpt
4 getBestPars
5 changeSaveFile
6 updateGP
然后,我可以手动创建一个“赋值箭头”:
header_text <- rep("<-")
然后,将其“粘贴”到每个函数名称:
combined_list <- as.character(paste(my_list, header_text, sep = ""))
但这看起来不正确:
combined_list
[1] "c(\"addIterations\", \"getLocalOptimums\", \"bayesOpt\", \"getBestPars\", \"changeSaveFile\", \"updateGP\")<- "
目标是使手动过程自动化 copying/pasting :
function_1 = getAnywhere("first function ParBayesianOptimization library")
function_2 = getAnywhere("second function ParBayesianOptimization library")
etc
final_list = c(function_1, function_2 ...)
并从每个函数中删除通用描述:
A single object matching ‘bayesOpt’ was found
It was found in the following places
package:ParBayesianOptimization
namespace:ParBayesianOptimization
with value
最后,如果我要“调用”final_list
对象,该库中的所有函数都应该重新创建并重新分配。
谁能告诉我怎么做?
谢谢
此代码将帮助您将库中所有函数的函数定义写入文本文件。
fn_list <- getNamespaceExports("ParBayesianOptimization")
for(i in seq_along(fn_list)) {
header <- paste('\n\n####Function', i, '\n\n\n')
cat(paste0(header, paste0(getAnywhere(fn_list[i]), collapse = '\n'), '\n\n'),
file = 'function.txt', append = TRUE)
}
您可以为此使用dump
函数
pkg <- "ParBayesianOptimization"
dump(getNamespaceExports(pkg), file="funs.R", envir = asNamespace(pkg))
我正在使用 R。我在 Whosebug 上找到了之前的 post,它显示了如何获取属于给定库的所有函数的“列表”:
How to find all functions in an R package?
例如:
#load desired library
library(ParBayesianOptimization)
#find out all functions from this library
getNamespaceExports("ParBayesianOptimization")
[1] "addIterations" "getLocalOptimums" "bayesOpt" "getBestPars" "changeSaveFile" "updateGP"
上面的代码告诉我在“ParBayesianOptimization”库中使用的所有函数的名称。从这里,我可以手动检查这些功能中的每一个 - 例如:
# manually inspect any one of these functions
getAnywhere(bayesOpt)
A single object matching ‘bayesOpt’ was found
It was found in the following places
package:ParBayesianOptimization
namespace:ParBayesianOptimization
with value
#function stats here
function (FUN, bounds, saveFile = NULL, initGrid, initPoints = 4,
iters.n = 3, iters.k = 1, otherHalting = list(timeLimit = Inf,
minUtility = 0), acq = "ucb", kappa = 2.576, eps = 0,
parallel = FALSE, gsPoints = pmax(100, length(bounds)^3),
convThresh = 1e+08, acqThresh = 1, errorHandling = "stop",
plotProgress = FALSE, verbose = 1, ...)
{
startT <- Sys.time()
optObj <- list()
etc etc etc ...
saveFile = saveFile, verbose = verbose, ...)
return(optObj)
}
#function ends here
<bytecode: 0x000001cbb4145db0>
<environment: namespace:ParBayesianOptimization>
目标 : 是否可以使用这些函数中的每一个并创建一个包含它们完整定义的记事本文件?
看起来像这样的东西:
我的尝试:
我想我可以先在 R 中创建一个“对象”,其中包含在该库中找到的所有函数:
library(plyr)
a = getNamespaceExports("ParBayesianOptimization")
my_list = do.call("rbind.fill", lapply(a, as.data.frame))
X[[i]]
1 addIterations
2 getLocalOptimums
3 bayesOpt
4 getBestPars
5 changeSaveFile
6 updateGP
然后,我可以手动创建一个“赋值箭头”:
header_text <- rep("<-")
然后,将其“粘贴”到每个函数名称:
combined_list <- as.character(paste(my_list, header_text, sep = ""))
但这看起来不正确:
combined_list
[1] "c(\"addIterations\", \"getLocalOptimums\", \"bayesOpt\", \"getBestPars\", \"changeSaveFile\", \"updateGP\")<- "
目标是使手动过程自动化 copying/pasting :
function_1 = getAnywhere("first function ParBayesianOptimization library")
function_2 = getAnywhere("second function ParBayesianOptimization library")
etc
final_list = c(function_1, function_2 ...)
并从每个函数中删除通用描述:
A single object matching ‘bayesOpt’ was found
It was found in the following places
package:ParBayesianOptimization
namespace:ParBayesianOptimization
with value
最后,如果我要“调用”final_list
对象,该库中的所有函数都应该重新创建并重新分配。
谁能告诉我怎么做? 谢谢
此代码将帮助您将库中所有函数的函数定义写入文本文件。
fn_list <- getNamespaceExports("ParBayesianOptimization")
for(i in seq_along(fn_list)) {
header <- paste('\n\n####Function', i, '\n\n\n')
cat(paste0(header, paste0(getAnywhere(fn_list[i]), collapse = '\n'), '\n\n'),
file = 'function.txt', append = TRUE)
}
您可以为此使用dump
函数
pkg <- "ParBayesianOptimization"
dump(getNamespaceExports(pkg), file="funs.R", envir = asNamespace(pkg))