如果忘记命名函数或对象,如何获得结果[重复]

时间:2021-03-15 16:56:09

This question already has an answer here:

这个问题在这里已有答案:

I have a very big function which takes hours to give me the result. I forgot to name it. Is there any way that I can show the result of my function?

我有一个非常大的功能,需要几个小时才能给我结果。我忘了给它起个名字。有什么方法可以显示我的功能结果吗?

Thanks in advance.

提前致谢。

1 个解决方案

#1


16  

You can rescue objects from drowning in the console with .Last.Value. See the following example.

您可以使用.Last.Value从控制台中淹没对象。请参阅以下示例。

sum(c(2,2,3,4))
#[1] 11
y <- .Last.value
y
#[1] 11

We learn from ?.Last.Value that

我们从?.Last.Value那里学习

The value of the internal evaluation of a top-level R expression is always assigned to .Last.value (in package:base) before further processing (e.g., printing).

在进一步处理(例如,打印)之前,总是将*R表达式的内部评估的值分配给.Last.value(在包:base中)。

This also works for functions:

这也适用于功能:

function(x){
  sqrt(x)
}

.Last.value
# function(x){
#   sqrt(x)
# }

Interesting note from lmo in the comments:

来自评论中的lmo有趣的说明:

As a side note, RStudio users can see this value in their environment panel by going to Tools > Global Options > General and then checking the box labelled "Show .Last.value in environment listing"

作为旁注,RStudio用户可以在其环境面板中看到此值,方法是转到工具>全局选项>常规,然后选中标记为“在环境列表中显示.Last.value”的框。

#1


16  

You can rescue objects from drowning in the console with .Last.Value. See the following example.

您可以使用.Last.Value从控制台中淹没对象。请参阅以下示例。

sum(c(2,2,3,4))
#[1] 11
y <- .Last.value
y
#[1] 11

We learn from ?.Last.Value that

我们从?.Last.Value那里学习

The value of the internal evaluation of a top-level R expression is always assigned to .Last.value (in package:base) before further processing (e.g., printing).

在进一步处理(例如,打印)之前,总是将*R表达式的内部评估的值分配给.Last.value(在包:base中)。

This also works for functions:

这也适用于功能:

function(x){
  sqrt(x)
}

.Last.value
# function(x){
#   sqrt(x)
# }

Interesting note from lmo in the comments:

来自评论中的lmo有趣的说明:

As a side note, RStudio users can see this value in their environment panel by going to Tools > Global Options > General and then checking the box labelled "Show .Last.value in environment listing"

作为旁注,RStudio用户可以在其环境面板中看到此值,方法是转到工具>全局选项>常规,然后选中标记为“在环境列表中显示.Last.value”的框。