2

The .Last.value command in R is useful for quick calculations, but the name is pretty long and difficult to write (probably because .Last.value should not be overwritten). Therefore, it is often faster to copy-paste the number from line above.

124/pi
# [1] 39.47043
.Last.value^2
# [1] 1557.915

I know that it is possible to do partial matching for functions, but for .Last.value this does not seem to work:

1+1
# [1] 2
.Last.v + 1
# Error: object '.Last.v' not found

Is there a handier (shorter/faster/better) way to draw the last value from R console?

Community
  • 1
  • 1
Mikko
  • 7,530
  • 8
  • 55
  • 92
  • 2
    Create a function like `LV <- function() .Last.value`? Then call it with `LV()` when required? – A5C1D2H2I1M1N2O1R2T1 Mar 23 '14 at 09:10
  • The classic answer to this is Verzani's comment here: http://stackoverflow.com/questions/4973185/r-equivalent-of-python/4973224#comment5564251_4973224 – Thomas Mar 23 '14 at 12:01

1 Answers1

6

You can use a closure to capture this and put it in your .Rprofile. For example:

lv = function () .Last.value

And then call lv() wherever you need to use .Last.value. Hopefully, you only use it in interactive mode and nowhere in scripting.

asb
  • 4,392
  • 1
  • 20
  • 30
  • Hah! @Ananda and I answered at almost the same time. – asb Mar 23 '14 at 09:11
  • +1 for the same idea then :-) – A5C1D2H2I1M1N2O1R2T1 Mar 23 '14 at 09:56
  • 1
    If four symbols is too long it is possible to make active binding: makeActiveBinding("a", lv, .GlobalEnv) After that just type "a" to see last value. – Gregory Demin Mar 23 '14 at 11:36
  • @asb This is the best solution until the R-studio team comes up with a quick double tap of up arrow to do the same trick or something =) Btw, I get your meaning, but the code does not work as it is. Cannot assign functions to `()`. Could you fix your answer to `lv <- function() .Last.value`? – Mikko Mar 23 '14 at 14:36
  • @Largh: Apologies. That was stupid. – asb Mar 23 '14 at 18:26
  • 2
    I use `makeActiveBinding(".", function() .Last.value, env = globalenv())` so I can use `.` like Ruby and Python use `_`. – Robert Krzyzanowski Mar 23 '14 at 20:15