简体   繁体   中英

R: Collect All Function Definitions from a Library

I am working with R. I found this previous post on stackoverflow which shows how to get a "list" of all functions that belong to a given library:

How to find all functions in an R package?

For example:

#load desired library
library(ParBayesianOptimization)

#find out all functions from this library
getNamespaceExports("ParBayesianOptimization")

[1] "addIterations"    "getLocalOptimums" "bayesOpt"         "getBestPars"      "changeSaveFile"   "updateGP" 

The above code tells me the name of all functions that are used in the "ParBayesianOptimization" library. From here, I could manually inspect each one of these functions - for example:

# 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>

Goal : Is it possible to take each one of these functions and create a notepad file with their full definitions?

Something that would look like this:

在此处输入图片说明

My attempt:

I thought I could first make an "object" in R that contained all the functions found in this library:

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

Then, I could manually create an "assignment arrow":

header_text <- rep("<-")

Then, "paste" this to each function name:

combined_list <- as.character(paste(my_list, header_text, sep = ""))

But this is not looking correct:

combined_list
[1] "c(\"addIterations\", \"getLocalOptimums\", \"bayesOpt\", \"getBestPars\", \"changeSaveFile\", \"updateGP\")<- "

The goal is to automate the process of manually copying/pasting :

function_1 = getAnywhere("first function ParBayesianOptimization library") 
function_2 = getAnywhere("second function ParBayesianOptimization library") 
etc

final_list = c(function_1, function_2 ...)

And removing the generic description from each function:

A single object matching ‘bayesOpt’ was found
It was found in the following places
  package:ParBayesianOptimization
  namespace:ParBayesianOptimization
with value

In the end, if I were to "call" the final_list object, all the functions from this library should get recreated and reassigned.

Can someone please show me how to do this? Thanks

You can use the dump function for this

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

This code will help you write the function definitions of all the functions in a library to a text file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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