对象创建时间戳
有没有办法检索对象的创建时间?
Is there a way to retrieve the time an object was created at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法检索对象的创建时间?
Is there a way to retrieve the time an object was created at?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
通常不会,但您可以对自己创建的对象执行此操作
,然后您可以自己编写一些使用该属性的自定义
print()
或show()
函数。 Frank Harrell 的 rms 及其设计前身长期以来一直在做类似的事情。Not generally but you can do it for objects you create yourself via
and you can then write yourself some custom
print()
orshow()
functions that use the attribute. Frank Harrell's rms and its Design predecessor have done something like that for a long time.简短回答:否。
详细回答:是的,您所需要做的就是重写 R 的 C 核心中的赋值代码,以便在每次更改对象时在某处存储日期戳。我尝试过一次,将数据存储在一个属性中,就像这里的其他答案一样,但它有一个不幸的副作用,即使相同的对象变得不同。 x=1 和 y=1 具有不同的时间戳,因此 Sametime(x,y) 为 FALSE,这极大地破坏了 R 的测试。我放弃了。
Short answer: No.
Long answer: Yes, all you need to do is rewrite the assignment code in R's C core to store a datestamp somewhere every time an object is changed. I tried this once, storing the data in an attribute much like other answers here, but it had the unfortunate side-effect of making identical objects different. x=1 and y=1 had different timestamps, so identical(x,y) was FALSE and that broke R's tests in magnificent ways. I gave up.
除了 Spacedman 的回答和我的评论之外,请参阅此示例:
您可能不想在
.GlobalEnv
中执行此操作,但它在本地化环境中可能很有用。Further to Spacedman's answer, and my comment there, see this example:
You probably wouldn't want to do this in
.GlobalEnv
, but it could be useful within a localized environment.我认为查尔斯的功能很棒,但它可能会在全球环境中产生问题。
我建议创建一个新的运算符
%c%
来代替<-
:另外两个函数可用于比较时间戳。 (使用后面的这些函数,我可以避免耗时的计算,除非某些参数对象已更改。)
I think that Charles' function is great, but it can create problems in global environment.
I suggest to create a new operator
%c%
to be used instead of<-
:The other two functions can be used to compare the time stamps. (With these latter functions I can avoid time-consuming calculations unless some argument objects have changed.)
您认为不改变对象,而是以与保存历史记录相同的方式保存时间戳怎么样? R 将控制台历史记录保存到您正在使用的文件夹中的文件
.Rhistory
中(即getwd()
)。我让 R 将时间戳(以 Unix 格式,自 Epoch = Jan 1st 1970 0:00:00 以来的秒数)保存到
.Rtimestamps
,但仅当将此设置为选项时:用法:
现在您只需要获取这些日期,您可以使用一些打印有效 POSIXct 类的辅助函数来完成此操作:
现在它的工作方式如下:
What do you think of not altering the objects, but instead save the timestamps the same way as history is being saved? R saves your console history to a file
.Rhistory
in the folder you're working in (i.e.getwd()
).I let R save timestamps (in Unix format, amount of seconds since Epoch = Jan 1st 1970 0:00:00) to
.Rtimestamps
, but only when this is set as an option:Usage:
Now you only need to get those dates, which you can do with some helper functions that print valid POSIXct classes:
And now it works like this: