我正在尝试从Mac中的Rstudio导出所有命令历史记录。但是在“历史”选项卡或使用savehistory(file = ".Rhistory")
时,只有大约500个最新命令被导出。然而,当我直接搜索命令时,我可以看到比这500个命令早得多的命令,例如半年前。那么Rstudio将这些历史存储在哪里呢?我怎么出口呢?如果最后的历史记录包含了我输入的命令的日期,那就更好了。非常感谢。
发布于 2022-06-10 23:49:25
在使用相同的问题进行搜索之后,下面是一个答案,以防其他人遇到相同的问题:
savehistory
是一个基本的R函数,它将保存当前会话中的所有内容,而RStudio有一个历史窗口使用的独立数据库。位置取决于您的操作系统(更多信息这里)。
Windows:%localappdata%\RStudio
macOS:open ~/.local/share/rstudio
一旦您找到了history_database
,这有用的代码by @维泽姆利允许您将内容导出到具有“更好的格式”(即每一行代码历史记录的数据和时间戳)的文本文件中:
library(dplyr)
library(magrittr)
library(lubridate)
library(bit64)
library(stringr)
lns <- readLines("yourfilelocation/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 %<>% dplyr::select(-epoch)
dd <- hist_db$day %>% unique %>% sort
ff <- "Database/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]) %>% dplyr::select(nice_date,history) %>% arrange(nice_date) %>%
write.table(ff,sep="\t", quote=F, row.names=FALSE, col.names=FALSE, append=TRUE)
}
示例输出:
--------------------------------------------------------------------------------
R history
--------------------------------------------------------------------------------
2022-06-08
--------------------------------------------------------------------------------
2022-06-08 13:32:57 test <- testfile %>% pivot_wider(names_from=scenario, values_from=sst) %>% na.omit()
2022-06-08 13:32:57 ggplot() + theme_bw() +
2022-06-08 13:32:57 geom_tile(data = test, aes(x,y,colour=layer), alpha=0.8) +
2022-06-08 13:32:57 scale_color_viridis() +
2022-06-08 13:32:57 coord_equal() +
2022-06-08 13:32:57 theme(legend.position="bottom")
--------------------------------------------------------------------------------
https://stackoverflow.com/questions/69758242
复制相似问题