Skip to content

savehistory not working properly in R?

5 messages · Paul Y. Peng, Brian Ripley, Patrick Connolly

#
Dear R users,

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? 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.

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

            
Yes.  The history is associated with the command-line editing: what you 
are doing is considered to be BATCH use.
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.
#
Prof Brian Ripley wrote:
Many thanks. As always, your suggestion works. I should have thought
about interactive().
I will give try() a real try. It seems to be quite useful.
Why does R save its command history only when one saves the data
image of the session? Is this a feature? Even though .Last can do
this, would it be more convenient if R can do this automatically?
Logically, I feel the command history should be treated separately.

Paul.
#
On Thu, 15 May 2003, Paul Y. Peng wrote:

            
I don't know: it predates my involvement with R.  The Windows ports used
always to save it, but we changed to conform to the Unix version, since
being different confused too many people.
#
On Thu, 15-May-2003 at 02:51PM -0230, Paul Y. Peng wrote:
|> Why does R save its command history only when one saves the data
|> image of the session? Is this a feature? Even though .Last can do
|> this, would it be more convenient if R can do this automatically?
|> Logically, I feel the command history should be treated separately.

I like it how it is.  I always have at least two R sessions active.
One I'm working on and the other I use for tinkering with ideas that
come up from reading this list.  I don't usually want to keep a
history of those tinkerings, so I quit the tinkering R session without
saving a data image.

best