7

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.

loki
  • 9,816
  • 7
  • 56
  • 82

1 Answers1

16

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

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

We learn from ?.Last.Value that

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).

This also works for functions:

function(x){
  sqrt(x)
}

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

Interesting note from lmo in the comments:

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"

Henrik
  • 65,555
  • 14
  • 143
  • 159
loki
  • 9,816
  • 7
  • 56
  • 82