R sometimes doesn't save my story

I have a program in R. Sometimes, when I save a story, they are not written to my history file. Several times I lost a few stories, and it really drove me crazy.

Any recommendations on how to avoid this?

+6
source share
4 answers

First check the working directory ( getwd() ). savehistory() saves the history in the current working directory. And frankly, you'd better specify the file name, since the default is .History . Say:

 savehistory('C:/MyWorkingDir/MySession.RHistory') 

which allows you to:

 loadhistory('C:/MyWorkingDir/MySession.RHistory') 

So, the story is not lost, it is just in place and under a name that you did not know about. See Also ?history .

To clarify: history is nothing more than a text file containing all the commands of this current session. So this is a good journal of what you have done, but I almost never use it. I myself create my "analysis log" using scripts, as another answer hinted at.

+7
source

@Stedy provided a workable solution to your immediate question. I would advise you to learn how to use .R files and the right text editor, or use the integrated development environment (see this SO page for suggestions). You can then source() in your .R file so that you can replicate your analysis sequentially.

For even better play, invest in Sweave training. You will be glad you did.

+1
source

Convenient registration of your console on ** obsolete files *. The TeachingDemos package has an excellent function for registering a console session, but it is written as single-line, which is problematic for automatic logging, since you cannot use this function to create a training demo if you use it for logging, I reused this function. using a bit of metaprogramming to make a copy of this function that I included in the .First function in my local .Rprofile , as shown below:

 .Logger <- (function(){ # copy local versions of the txtStart, locStart <- TeachingDemos::txtStart locStop <- TeachingDemos::txtStop locR2txt <- TeachingDemos:::R2txt # creat a local environment and link it to each function .e. <- new.env() .e.$R2txt.vars <- new.env() environment(locStart) <- .e. environment(locStop) <- .e. environment(locR2txt) <- .e. # reference the local functions in the calls to `addTaskCallback` # and `removeTaskCallback` body(locStart)[[length(body(locStart))-1]] <- substitute(addTaskCallback(locR2txt, name='locR2txt')) body(locStop)[[2]] <- substitute(removeTaskCallback('locR2txt')) list(start=function(logDir){ op <- options() locStart(file.path(logDir,format(Sys.time(), "%Y_%m_%d_%H_%M_%S.txt")), results=FALSE) options(op) }, stop = function(){ op <- options() locStop() options(op) }) })() .First <- function(){ if( interactive() ){ # JUST FOR FUN cat("\nWelcome",Sys.info()['login'],"at", date(), "\n") if('fortunes' %in% utils::installed.packages()[,1] ) print(fortunes::fortune()) # CONSTANTS TIME <- Sys.time() logDir <- "~/temp/Rconsole.logfiles" # CREATE THE TEMP DIRECORY IF IT DOES NOT ALREADY EXIST dir.create(logDir, showWarnings = FALSE) # DELETE FILES OLDER THAN A WEEK for(fname in list.files(logDir)) if(difftime(TIME, file.info(file.path(logDir,fname))$mtime, units="days") > 7 ) file.remove(file.path(logDir,fname)) # sink() A COPY OF THE TERMINAL OUTPUT TO A DATED LOG FILE if('TeachingDemos' %in% utils::installed.packages()[,1] ) .Logger$start(logDir) else cat('install package `TeachingDemos` to enable console logging') } } .Last <- function(){ .Logger$stop() } 

This results in a copy of the contents of the terminal being copied to the dated log file. The best part is that you have date files, that if you use multiple R sessions, the log files will not conflict, unless you start several interactive sessions in one second).

0
source

According to the documentation, save.image() is short for "save current workspace" and will probably be your best choice based on your question.

-1
source

Source: https://habr.com/ru/post/885689/


All Articles