将自定义包的所有函数导出到一个文件中。

huangapple go评论64阅读模式
英文:

Exporting all functions of a custom package to a file

问题

我的情况如下:
我创建了一个名为MyCDM的包,其中包含了几个我在我的应用程序中经常使用的函数。当我在我的个人电脑上运行脚本(我使用Rstudio时),我包含了library(MyCDM)命令,一切都正常。

问题是,我想迁移到我大学的云服务上执行我的脚本,但我没有包括新包的特权。

我想要将MyCDM包的所有函数导出到一个单独的R文件中,这样我可以将这个文件复制到大学服务器上并在我的脚本中源化它。

我知道我可以复制我的包的“R”文件夹并粘贴到目标文件夹中。但大学网站建议我们避免复制/读取/写入大量小文件,因为这会影响服务器的性能。

我已经成功将所有函数导入到我的全局环境中,但我不知道如何将函数体写入文件中。

有没有一种简单的方法来做到这一点?

英文:

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)

答案1

得分: 2

一个解决方法是只使用R将R目录中每个文件的内容复制并粘贴到一个新文件中:

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

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

答案2

得分: 1

你可以使用 lsf.str("package:MyCDM") 访问“MyCDM”中的所有函数列表。然后,您可以将其传递给 dump() 以将所有内容保存在一个 R 文件中:

dump(lsf.str("package:MyCDM"), file="MyCDM functions.R")

稍后,您只需执行:

source("MyCDM functions.R")

所有函数将加载到您的全局环境中。

英文:

You can acess the list of all functions in "MyCDM" with lsf.str("package:MyCDM"). Then, you can pass that to dump() to save it all in a R. file:

dump(lsf.str("package:MyCDM"), file="MyCDM functions.R")

Later, you just need to do

source("MyCDM functions.R")

And all your functions will be loaded in your global enviroment.

huangapple
  • 本文由 发表于 2023年6月11日 23:58:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定