32

Python has an identifier _ that allows for storing the result of the last evaluation which makes it great for speeding up data exploration and introspection.

In [1]: 43 * 2
Out[1]: 86

In [2]: _ + 1
Out[2]: 87

Is there a similar command in R?

2
  • 2
    @David - For what it's worth, the standard python interpreter does the same thing when used interactively. It's not just ipython. Commented Feb 11, 2011 at 19:47
  • docs.python.org/tutorial/introduction.html#numbers toward the bottom of the section: "In interactive mode, the last printed expression is assigned to the variable _."
    – nmichaels
    Commented Feb 11, 2011 at 19:51

1 Answer 1

44

Tis a faff to type, but .Last.value:

> sqrt(2)
[1] 1.414214
> .Last.value
[1] 1.414214
2
  • 1
    I agree that the name is difficult to remember. You could do this:.Last() function() (return(.Last.value)); a <- 3 #> .Last() [1] 3
    – IRTFM
    Commented Feb 11, 2011 at 19:44
  • 27
    Try this: makeActiveBinding(".",function() .Last.value,env=.GlobalEnv) then "." is the value, similar to "_".
    – jverzani
    Commented Feb 13, 2011 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.