Skip to content

Instantly share code, notes, and snippets.

@vzemlys
Created February 9, 2015 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vzemlys/751887b31657649a6a5c to your computer and use it in GitHub Desktop.
Save vzemlys/751887b31657649a6a5c to your computer and use it in GitHub Desktop.
Save Rstudio history database into a nicer format
library(dplyr)
library(magrittr)
library(lubridate)
library(bit64)
library(stringr)
lns <- readLines("~/.rstudio-desktop/history_database") %>% str_split(pattern=":",n=2)
hist_db <- data_frame(epoch=as.integer64(sapply(lns,"[[",1)),history=sapply(lns,"[[",2))
hist_db %<>% mutate(nice_date = as.POSIXct(epoch/1000,origin = "1970-01-01",tz = "EET"))
hist_db %<>% mutate(day = ceiling_date(nice_date,unit = "day")-days(1))
hist_db %<>% select(-epoch)
dd <- hist_db$day %>% unique %>% sort
ff <- "~/R/hist_nice.txt"
cat("R history","\n",rep("-",80),"\n",file=ff,sep="")
for(i in 1:length(dd)) {
cat("\n\n",format(dd[i]),"\n",rep("-",80),"\n",file=ff,sep="",append=TRUE)
hist_db %>% filter(day==dd[i]) %>% select(nice_date,history) %>% arrange(nice_date) %>%
write.table(ff,sep="\t", quote=F, row.names=FALSE, col.names=FALSE, append=TRUE)
}
@dainiuxt
Copy link

Vaidas,
maybe you have any clue why I get an error:

hist_db <- data_frame(epoch=as.integer64(sapply(lns,"[[",1)),history=sapply(lns,"[[",2))
Error in FUN(X[[1L]], ...) : subscript out of bounds

@vzemlys
Copy link
Author

vzemlys commented Feb 13, 2015

This script assumes that the rstudio history file lives in a specified place and has the following structure:

TimeStamp:history_line

If you get this error either the file is not there or the history file is corrupted.

@whizzalan
Copy link

Thanks a lot!
It's useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment