在 R 交互式会话中,如何访问最近表达式的值?

In an R interactive session, how can I access the value of the most recent expression?

当我在与 R 的交互式会话中并键入命令(例如,log(25))时,解释器会显示结果

> log(25)
[1] 3.218876
>

该结果是否可用作我可以在下一行使用的某些特殊变量的值?

.Last.value 这样做:

> .Last.value
$help_type
NULL

> 5
[1] 5
> .Last.value
[1] 5
> iris; .Last.value

但是我真的不知道,是否应该使用它。只需给事物一个明确的名称。写起来需要更少的击键

> (a <- 5)
[1] 5
> a
[1] 5

然后每个人都可以很容易地看到发生了什么,如果您稍后返回到您的脚本并输入额外的一行,那不会造成伤害。

来自Zen of Python

Explicit is better than implicit.

Simple is better than complex.

Readability counts.

Special cases aren't special enough to break the rules.

If the implementation is hard to explain, it's a bad idea.