0

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:

enter image description here

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

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 1
    Take a look at the `dump()` function. It can export the code for functions if you pass in their names as a list. But it looks like you are only looking at exported functions and some functions might call unexported functions which would then break if you tried to use those function. If you open up the tar.gz package file, you can get all the source code for all the R functions in the "R" folder. It's not clear to me what exactly you are trying to accomplish by redefining all the functions. – MrFlick Jul 16 '21 at 06:55
  • thank you for your reply! I am just trying to obtain the function definition for every function with a specific library. I understand that there might be unexported functions which might break if you call them - but just as a learning excursive, I was interested in seeing if there was an automatic way of obtaining the code for each function within the library (e.g. please see the screenshot I uploaded). thank you so much for your help! – stats_noob Jul 16 '21 at 06:58

2 Answers2

3

You can use the dump function for this

pkg <- "ParBayesianOptimization"
dump(getNamespaceExports(pkg), file="funs.R", envir = asNamespace(pkg))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is also so INCREDIBLE! This appears in the R environment just the way I needed! I am in pure AWE! Thank you so much! – stats_noob Jul 16 '21 at 07:06
1

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)
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • WOW! this is amazing! Thank you so much! Just a question: is it possible to remove some of the text that appears at the start and at the end of each function? Something that looks like this? https://imgur.com/a/jXp1ykm Thank you so much! This code is so beautiful that you have provided! – stats_noob Jul 16 '21 at 07:04
  • 1
    I think @MrFlick's approach is much better. You can just `source('funs.R')` to use all these functions offline. – Ronak Shah Jul 16 '21 at 07:08