2

I have a vector x <- rnorm(100) and whilst exploring my data I do:

x

Now I want to look at the mean of x, for which I could do:

mean(x)

But lets say instead I want to have a function last() which pulls in the last thing done in Rs history and saves it as an object, which would enable me to do:

mean(last())

I'd like to write a last() function if it doesn't already exist. But I cannot find a way to access (without saving and then reading back in) the history in any useful way via history().

Am I missing something?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Andrew Taylor
  • 3,438
  • 1
  • 26
  • 47

1 Answers1

4

You can get this working using .Last.value:

lv <- function() .Last.value
x = runif(100)
mean(lv())

I would be ok with using this in interactive mode, but using this in normal mode would make the code hard to read imo. So use at your own discretion.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • 2
    And to save even more keystrokes, try `makeActiveBinding(".", function() .Last.value, env=.GlobalEnv) ` Then `mean(.)` – DaveTurek Sep 23 '15 at 11:57