[R] savehistory not working properly in R?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 15 08:39:27 CEST 2003


On Wed, 14 May 2003, Paul Y. Peng wrote:

> I have a .RData which contains .Last object as follows:
> 
> 	.Last <- function () 
> 	{
> 		savehistory(file = ".Rhistory")
> 	}
> 
> In this directory, if I issue the following command
> 
> 	Rterm --save < mycmds.q
> 
> to execute commands in mycmds.q and to save results in .RData,
> I got the following error message towards the end of the execution:
> 
> 	Error in savehistory(file) : savehistory can only be used
> 	in Rgui and Rterm
> 	Execution halted
> 
> I lost the results of the commands too. The documentation of
> "savehistory" says that it only works with Rgui and Rterm. Why
> doesn't it work in this case? Is it because it is in a batch mode
> rather than in an interactive mode? 

Yes.  The history is associated with the command-line editing: what you 
are doing is considered to be BATCH use.

> I did this in Windows98 with
> the following R
> 
> 	> version
> 	platform i386-pc-mingw32
> 	arch     i386           
> 	os       mingw32        
> 	system   i386, mingw32  
> 	status                  
> 	major    1              
> 	minor    6.2            
> 	year     2003           
> 	month    01             
> 	day      10             
> 	language R
> 
> I tested it in Linux with the same version R and the same problem
> happened.
> 
> Is it possible to prevent savehistory from printing the error message
> so that the session can be finished properly? I hope that I did not
> miss anything obvious. Otherwise, please forgive me. Thanks.

The real problem is in your .Last.  You could do

.Last <- function ()
{
    if(interactive()) savehistory(file = ".Rhistory")
}

or use try():  if you are worried about not saving .RData you really 
should protect the commands in .Last (see its help page for warnings).

I'll see if the documentation can be clarified.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list