Is it possible to retrieve the console output history in RStudio?

Viewed 857

The title pretty much says it all.

I left a process running overnight and used the tictoc package to measure the duration of it.

When I checked back this morning, I read the output then cleared the console as I always do (muscle memory) to keep things tidy.

Even though I remember the duration, I would like to share the details of the output with a colleague and rather than running the process again (it took a long time), I wonder if it is possible to retrieve just the output of the console.

There don't appear to be any similar questions on Stack Overflow, so I decided to pose one.

I am aware that it is possible to retrieve one's own history (history()) but that only focuses on inputs whereas I am looking for outputs.

Additional detail:

zx8754's answer is good, although it only works for the previous result, whereas I need something that looks further back.

Since clearing the console, I have run several commands, meaning that .Last.value won't work in this case. Is there an approach that looks further back than the most recent output?

1 Answers

Looks like .Last.value works, example:

library(tictoc)

## Timing multiple steps
tic("step 1")
print("Do something...")
Sys.sleep(1)
toc()
# step 1: 1.02 sec elapsed

# Press Ctrl+L to clear console, then
.Last.value
# $tic
# elapsed 
# 239407.9 
# 
# $toc
# elapsed 
# 239408.9 
# 
# $msg
# [1] "step 1"

Print out is not the same, but we can re-construct if needed:

x <- .Last.value
paste0(x$msg, ": ", prettyNum(x$toc - x$tic), " sec elapsed")
# [1] "step 1: 1.02 sec elapsed"
Related