繁体   English   中英

R:从库中收集所有函数定义

[英]R: Collect All Function Definitions from a Library

我正在使用 R。我在 stackoverflow 上找到了上一篇文章,它展示了如何获取属于给定库的所有函数的“列表”:

如何找到R包中的所有函数?

例如:

#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\")<- "

目标是自动化手动复制/粘贴的过程:

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对象,则该库中的所有函数都应该重新创建和重新分配。

有人可以告诉我如何做到这一点吗? 谢谢

您可以为此使用dump功能

pkg <- "ParBayesianOptimization"
dump(getNamespaceExports(pkg), file="funs.R", envir = asNamespace(pkg))

此代码将帮助您将库中所有函数的函数定义写入文本文件。

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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM