如何修改R语言save()函数以保存对象的源脚本属性
自定义R的
save()函数,添加对象源脚本属性 如果你想让R的保存操作自动记录对象来自哪个脚本,这里有个实用的自定义函数——它会先给目标对象添加一个名为original.script的属性来存储源脚本名称,再执行常规的保存操作:
s = function(object, filepath, original.script.name){ # modified save() function # stores the name of the script from which the object originates as an attribute, then saves as normal attr(object, "original.script") = original.script.name save(object, file = filepath) }
使用示例
用一个简单的向量来测试这个自定义函数:
# 创建测试对象 testob = 1:10 testob # 输出结果: [1] 1 2 3 4 5 6 7 8 9 10 # 调用自定义函数保存,指定源脚本名称 s(testob, filepath = "rotation1scripts_v4.RData", original.script.name = "my_analysis_script.R")
之后加载这个保存的对象时,只需通过attr(testob, "original.script")就能查看它的源脚本名称啦。
内容的提问来源于stack exchange,提问作者Thistle19
相关产品推荐
相关产品推荐

