Skip to content
Prev 4466 / 15075 Next

savehistory in OSX version

Thanks, Rob.

In the interests of wasting many hours to save a few seconds of hateful
clicking around,
I have written an applescript solution for saving the history.
It works (most of the time).
Why I want it:  (a) automate my paranoia,  (b) sometimes, to load
current history into a script window
to refine and save some juicy section of code.
(The latter can be done with loadHistory(file.choose()) followed by
history().)

The applescript below assumes 
	--	1.  there is a file "~/cred.Rdata" that contains
username and password as string objects (could be safer!)
			(This is not necessary if you always have
Universal Access turned on.)
	--  	2.  there is the following function on your search path:

	saveHistory=function(verbose=FALSE, echo=FALSE) {
		if(is.na(match("file:~/cred.Rdata", search())))
			attach("~/cred.Rdata")
		if(verbose == FALSE)verbose = "false"   ## arg in 
		else	verbose = "true"
		the.command = paste(
			"nohup /usr/bin/osascript
~/R-applescript/saveHistory.app ",  
			Sys.Date(), ".history ", username, " ",
password, " ", verbose, " &\n", sep="")
		## the "nohup" helps a lot
		if(echo) cat(the.command)
		system(the.command)
	}

	--   	3.  	you prefer "universal access" to be disabled
after this is done (this is easy to change).
	--	4. 	there is a compiled applescript file
~/R-applescript/saveHistory.app containing the following: 

on run {historyfilename, myUserName, myPassword, verbose}
	-- TO DO handle the "replace existing" case (but maybe it's
better this way!)
	-- TO DO figure out why it is so SLOOOW-- much better if no
"say"s.  (verbose=false)
	-- TO DO sometimes the accessibility fix doesn't work even
though .AccessibilityAPIEnabled was created .
	--       If so, set it by hand, using System
Preferences/Universal Access.
	my enableAccessibility(myUserName, myPassword 
	if (verbose = true) then say "this is the app version"
	tell application "System Events"
		tell process "R.app"
			if not (exists drawer 1 of window "R Console")
then
				if (verbose = true) then say "beginning
to open the drawer"
				click button "History" of tool bar 1 of
window "R Console"
				if (verbose = true) then say "opening
the drawer"
				-- delay 1
			end if
			if (verbose = true) then say "Now I am clicking
the Save History button"
			click button "Save History" of drawer 1 of
window "R Console"
			if (verbose = true) then say "Clicked."
			tell window "Save history File"
				if (verbose = true) then say "history
file name is " & historyfilename
				--click text field 1
				set value of text field 1 to
historyfilename
				--  item 1 of argv is just the first
letter
				if (verbose = true) then say "file name
is inserted"
				-- It's necessary to enable the Save
button by clicking the dropdown button.
				-- click text field 1  does not do the
job.  Therefore...
				if title of button 3 = "Cancel" then
					-- file list is expanded-- hide
it!
					click button 4
				else
					-- file list is hidden-- expand
it!
					click button 3
				end if
				click button "Save"
				-- An overwrite dialog might now appear.
				-- This is left for user interaction.
			end tell
		end tell
	end tell
	my disableAccessibility(myUserName, myPassword)
end run

on enableAccessibility(myUserName, myPassword)
	do shell script "echo a >
/private/var/db/.AccessibilityAPIEnabled" user name myUserName password
myPassword with administrator privileges
end enableAccessibility

on disableAccessibility(myUserName, myPassword)
	try
		do shell script "/bin/rm
/private/var/db/.AccessibilityAPIEnabled" user name myUserName password
myPassword with administrator privileges
	end try
end disableAccessibility


---------------------------

Enjoy, but no warranty of perfection or promise of support is expressed
or implied!

-----------------
Roger Day
University of Pittsburgh Departments of Biomedical Informatics and 
Biostatistics 
University of Pittsburgh Cancer Institute 
University of Pittsburgh Molecular Medicine Institute


-----Original Message-----
From: Rob Goedman [mailto:goedman at mac.com] 
Sent: Monday, January 14, 2008 7:27 PM
To: Day, Roger S.
Cc: r-help at r-project.org
Subject: Re: [R] savehistory in OSX version

Roger,
On Jan 14, 2008, at 1:14 PM, Day, Roger S. wrote:

            
Right now, there isn't in R.app.
The history file is updated on exiting R, which is clearly of no use to
you. The updated history file can be read by both R and R.app, this is
the interoperable part.

If I recall correctly (it has been a few years!), to do what you need,
would require a call back from R. I seem to remember this would not be
hard to do but at that time was not available. Don't think it is today
either.

A possible approach is to run long interactive sessions from within the
editor. Add a couple of statements and submit selection to R. If you're
using the internal editor, save the file before submitting. From within
an external editor, saving is less critical.

Rob