How to assign the result of the previous expression to a variable?

19,149

.Last.value is an answer.

It was answered once but you have better title.

Share:
19,149
Ryan C. Thompson
Author by

Ryan C. Thompson

Updated on July 12, 2022

Comments

  • Ryan C. Thompson
    Ryan C. Thompson almost 2 years

    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()
    
  • Daniel Que
    Daniel Que over 9 years
    Is there a more concise expression for this, or a way to configure one?
  • Ryan C. Thompson
    Ryan C. Thompson over 9 years
    lv <- function() .Last.value. Then use lv() to get the last value.
  • Paul FREAKN Baker
    Paul FREAKN Baker about 7 years
    I like the use of this lambda, but I used ans instead of lv so I could mimic my TI calculator.
  • Martin Ueding
    Martin Ueding almost 7 years
    One could type .La and hit the Tab key, that would also work.
  • lmo
    lmo over 6 years
    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."
  • stevec
    stevec over 4 years
    .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 ?