94

Suppose I'm using R's interactive console, and I've just done something like this:

long_running_command()

That long-running command returns a value, and I've just realized that I wanted to assign that value to a variable instead of discard it. So how can I get that value without running the command again? Is there a command like this?

result = get_last_return_value()
Jaap
  • 81,064
  • 34
  • 182
  • 193
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

1 Answers1

123

.Last.value is an answer.

It was answered once but you have a better title.

Mark
  • 7,785
  • 2
  • 14
  • 34
Marek
  • 49,472
  • 15
  • 99
  • 121
  • 3
    Is there a more concise expression for this, or a way to configure one? – Daniel Que Oct 27 '14 at 19:13
  • 8
    `lv <- function() .Last.value`. Then use `lv()` to get the last value. – Ryan C. Thompson Nov 25 '14 at 20:39
  • I like the use of this lambda, but I used `ans` instead of `lv` so I could mimic my TI calculator. – Niko May 01 '17 at 05:15
  • 3
    One could type `.La` and hit the Tab key, that would also work. – Martin Ueding Jul 31 '17 at 14:01
  • 3
    As a side note, rstudio users can see this value in their environment panel by going to `Global options > General` and then clicking on the box labelled "Show .Last.value." – lmo Dec 07 '17 at 13:48
  • 1
    `.Last.value` is very cool. Out of interest. Is there a short hand way to call it? I know ruby has the incredibly succinct `_`, which does the same as `.Last.value` in one character! - extremely convenient. Is there any short hand for `.Last.value` ? – stevec Aug 30 '19 at 16:55