4

Is it possible to save the whole sequence of commands from a specific day, from RStudio, into a file? If yes, how?

nbro
  • 15,395
  • 32
  • 113
  • 196
user30314
  • 183
  • 1
  • 11

1 Answers1

4

RStudio saves the history in ~/.rstudio-desktop/history_database (*NIX).

It saves the line of code and a running id which is seconds/1000 till epoch.

cmp as.numeric(Sys.time()).

So, as.numeric(Sys.time()-60*60*24)*1000 is the about the time index 24h back in time. However, the location of the history_database file may be platform dependent.

To me the following worked:

  # get the file to table
  h<-read.table("~/.rstudio-desktop/history_database",sep=":",fill=T,stringsAsFactors=F) 

  # convert timestamps to numeric, note that some are converted to NA
  h$V1<-as.numeric(h$V1)

  # enter time from when on you want to have your history
  from<-as.numeric(as.POSIXct("2014-03-27 10:00:00 CET"))*1000

  # accordingly
  to<-as.numeric(as.POSIXct("2014-03-27 13:00:00 CET"))*1000

  # I also want the lines with NA timestamps within my time window
  min<-min(which(h$V1>from & h$V1<to))
  max<-max(which(h$V1>from & h$V1<to))

  # this are lines you typed between 10:00 and 13:00  on 27th of march 2014
  h$V2[min:max]

  # save to file
  f<-file("history.txt")
  writeLines(h$V2[min:max], f)
  close(f)
nbro
  • 15,395
  • 32
  • 113
  • 196
Janhoo
  • 597
  • 5
  • 21
  • Very helpful. Although, the call to read.table won't work if you use ':' in your code for other purposes. I found readLines() and regex extraction to be the most generic way to separate timestamps and code lines. – Gopala Dec 01 '15 at 15:02