Search code examples
rpackage

Exporting all functions of a custom package to a file


My situation is the following: I made a package (named MyCDM) with several functions that I constantly use in my applications. When I running the scripts in my PC (I use Rstudio), I include the command library(MyCDM) and all works fine.

The problem is that I wanted to migrate the execution of my script to a cloud service of my University, where I don't have the privilege of include new packages.

I want to export all functions of MyCDM package into a single R file, in a way that I can copy this file into the University server and source it in my script.

I know that I could copy the "R" folder of my package and paste it on the destination folder. But the University site guides us to avoid to copy/read/write operations with a large number of small files due to the impact the server's performance.

I was able to import all functions to my global environment, but I don't know how to write the function body's into a file.

There is a way to do it in a simple manner?

(ps.: English is not my first language. Sorry for any language mistake. If I not made myself clear, please ask for clarification in the comments)


Solution

  • One solution is to just use R to copy and paste the contents of each file in the R directory into a new file:

    purrr::walk(
      .x = list.files(path = "R/"),
      .f = ~write(
        readLines(file.path("R", .x)),
        file = "functions_script.R",
        append = TRUE)
    )
    

    This gets a list of all files in the R directory (assuming your working directory is currently the root of the package, although you could easily change this) using list.files. Then copies and writes the contents of each file to a new file functions_script.R, (and appends each additional file).