should .RData files be used to store functions?

I use .RData files to store objects (eg lists, vectors, etc) then call them into other scripts, but I'm wondering whether they should also be used to store functions (most likely user-defined functions)?

I know source() is generally recommended for this purpose (and creating packages even more so), but an advantage as I see it is that a single .RData file can contain multiple objects - a list, dataframe, and the function, for example. Saves needing to call objects using load() , then the function separately, using source() .

Are there reasons to be cautious of this approach, that I'm not seeing?

Thank you


At my old job, we used to serialize out closures:

> f <- (function(x) function() x)(2)
> f()
[1] 2
> saveRDS(f, file='/tmp/f')

and then

> f <- readRDS('/tmp/f')
> f()
[1] 2

This can let you bundle data (eg coefficients) with a function. Be careful though, your libraries won't get autoloaded.

链接地址: http://www.djcxy.com/p/6536.html

上一篇: 在r caret包中的train功能的模型输出中有巨大的尺寸

下一篇: 应该使用.RData文件来存储函数?