Barbara
Barbara

Reputation: 347

Accessing R history from R code

I am aware of the history function that displays the R history in a separate panel for the user to see. What I am interested in is slightly different. I would like to be able to access the R history from R code itself, something where I could ask for the last n commands and get on return a vector containing those commands. Does anything like that exist?

Upvotes: 3

Views: 744

Answers (1)

lukeA
lukeA

Reputation: 54237

tempfile <- tempfile(pattern="rhistory_", fileext=".txt")
savehistory(tempfile) 
h <- readLines(tempfile)  

tail(h, 5) # display last 5 commands
eval(parse(text=h[length(h)-1])) # exec last command but 1

Upvotes: 3

Related Questions