将所有函数保存在一个txt文件中 [英] Save all functions in an txt file

查看:46
本文介绍了将所有函数保存在一个txt文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在 R 中运行大量模拟.在模拟之间,R 的某些部分代码会改变.通常,我一直在一起模拟结果包含一个 .txt 文件其中使用的每个函数的定义模拟.为了制作那个 .txt 文件,我只是运行这一行:

I typically run a lot of simulations in R. In between simulations, some parts of the R code would change. Typically, i keep alongside the simulation results a .txt file containing the definition of every function used in that simulation. To make that .txt file, i simply run this line:

for(j in 1:length(ls()))    print(c(ls()[j],eval(as.symbol(ls()[j]))))
out<-capture.output(for(j in 1:length(ls()))    print(c(ls()[j],eval(as.symbol(ls()[j])))))
cat(out,file=paste("essay_4_code.txt",sep=""),sep="\n",append=FALSE)

在我的环境中加载所有函数之后.然而,在生成的文本文件中,R 函数不是 R 可以解释为函数的格式.为了理解原因,这里有一个简单的例子:

right after loading all the function in my env. In the resulting text file however the R functions are not in a format that R can interpret as functions. To understand why, here is a simple example:

rm(list=ls())
foo1<-function(x){
  sin(x)+3
}
foo2<-function(x){
  cos(x)+1
}
foo3<-function(x){
  cos(x)+sin(x)
}

会产生:

[[1]]
[1] "foo1"

[[2]]
function (x) 
{
    sin(x) + 3
}

[[1]]
[1] "foo2"

[[2]]
function (x) 
{
    cos(x) + 1
}

[[1]]
[1] "foo3"

[[2]]
function (x) 
{
    cos(x) + sin(x)
}

所以,简而言之,我想让essay_4_code.txt R 可读

So, in a nutshell, i would want to make essay_4_code.txt R-readable

推荐答案

你可以使用

dump(lsf.str(), file="essay_4_code.R")

这将创建一个 .R 文件,其中包含当前搜索空间中的所有函数定义.

This will create an .R file with all of the function definitions in the current search space.

来自@JoshuaUlrich 在评论中发布的相关问题:

from the related question posted by @JoshuaUlrich in the comments:

...dump("f") will only save the function definition of f, and not its environment. 
If you then source the resulting file, f will no longer work correctly [if it 
depends on variables in the environment in was previously bound to].

或者,您可以使用 save 函数以二进制格式保存函数,该格式可以通过 load 函数读取.这会保留您函数的环境绑定,但您将无法自己读取生成的文件.

Alternatively, you can you use the save function to save the function in a binary format readable with the load function. This preserves your functions' environment bindings, but you lose the ability to read the resulting file yourself.

 do.call(save, c(as.list(lsf.str()), file='essay_4_code.Rd'))

在新会话中加载时,先前绑定到全局环境的函数将绑定到当前全局环境,而绑定到不同环境的函数将携带该环境.

Upon loading in a fresh session, functions previously bound to the global environment will be bound to the current global environment, while functions that were bound to a different environment will carry that environment with them.

rm(list=ls())
# function bound to non-global environment
e <- new.env()
e$x <- 10
f <- function() x + 1
environment(f) <- e
# function bound to global environment
y <- 20
g <- function() y + 1
# save functions
do.call(save, c(as.list(lsf.str()), file='essay_4_code.Rd'))

# fresh session
rm(list=ls())

load('essay_4_code.Rd')
f()
# [1] 11
g()
# Error in g() : object 'y' not found
y <- 30
g()
# [1] 31
ls()
# [1] "f" "g" "y"

如果您只想检查eassay_4_code.Rd"中的函数体:

And if you just wanted to inspect the bodies of the function in 'eassay_4_code.Rd':

e<-new.env()
load('essay_4_code.Rd', e)
as.list(e)
# $f
# function () 
# x + 1
# <environment: 0x000000000a7b2148>
# 
# $g
# function () 
# y + 1

这篇关于将所有函数保存在一个txt文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆