摘抄一下The Ghost Printer Behind Top-level R Expressions《藏在顶层R表达式背后的鬼魅打印函数》(lol)一文中的关键几句话:
(1)
A top-level R expression is usually implicitly printed.
For example, when you type 1 + 1 in the R console, and press Enter/Return,
what actually happens is print(2), where 2 is the value returned by 1 + 1.
就是说在控制台里输入了一个顶级表达式以后,会发生这么一件事:系统自动把表达式的返回值带入print()函数中,并显示结果。
(2)
Expressions nested in other expressions are not top-level expressions.
嵌套于其它表达式中的表达式,就不是顶级表达式了。一般而言,(1)就不再成立,也就是说非顶级表达式的值不会自动显示。
(3)
Note for, while, and repeat loops always return an invisible NULL, so nothing
will be printed automatically, including the last expression in the loop.
循环函数的返回值都是空,所以就算带入了print()函数也啥都显示不了。循环体内的表达式都不是顶级表达式,由(2),也啥都显示不了。
2019-02-08 感谢yihui 指出,(3)这句话写的有歧义。应该说是
循环函数的返回值都是空,所以作为顶层表达式,就算带入了隐含的print()函数也啥都显示不了。循环体内的表达式都不是顶级表达式,由(2),也不会自动打印。当然循环体中明确使用print()函数的地方,都会进行相应的输出。