user30314
user30314

Reputation: 193

How to save the whole sequence of commands from a specific day to a file?

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

Upvotes: 4

Views: 2057

Answers (1)

Janhoo
Janhoo

Reputation: 597

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)

Upvotes: 4

Related Questions