[ESS] 2 things I wish ESS would do

David Whiting david.whiting at ncl.ac.uk
Tue May 6 16:28:15 CEST 2008


Dear Paul and others,


On Wed, Apr 30, 2008 at 06:31:56PM +0100, Stephen Eglen wrote:
> 
> Dear Paul,

[...]

>  > 2. Can you make it easier to transfer commands that work from the *R*
>  > window back to the script file "blob.R" ?  Once we do hack our way to
>  > success in the *R* file, we want to get those commands back and save
>  > them.  At the current time, the only way I can find is to highlight
>  > the whole section of the *R* buffer, copy it over to "blob.R" and then
>  > delete all the lines that don't start with > and edit the rest.


I have found myself often wanting to do this as well, and needed to
again today. So, based on Stephen's outline, I have hacked some code
together. See below.

> The transcript editing code might be useful here.  e.g. given some
> region of *R* output like
> 
> > x <- rnorm(2)
> > x + 3
> [1] 3.091824 4.764668
> > x - 1
> [1] -0.9081763  0.7646682
> > 
> if I copy & paste that into a buffer called "foo.Rt" and then make the
> buffer writeable (C-x C-q) and then do C-c C-w
> (ess-transcript-clean-region) on the whole buffer, I get:
> 
> x <- rnorm(2)
> x + 3
> x - 1
> 
> It should be straightforward to write a defun that takes a marked
> region in a *R* buffer, copies it to some temp buffer (e.g. called foo.Rt)
> and then runs ess-transcript-clean-region.
> 
> However, where would you then want the contents of foo.Rt copied?  In
> your case, you would say blob.R, but what if the *R* process is being
> used by several .R files [at least I often associate one *R* process
> with several buffers].  Some heuristic could be used, e.g. stick it at
> point in the most recent .R buffer associated with that process.
> 
> would that work for you?
> 
> Stephen

The following attempts to implement part of Stephen's idea. It does
not attempt to figure out where to place the code, it just gets as far
as removing the output and copying the remaining code. I couldn't
figure out how to tell emacs not to ask if I want to save foo.Rt. Now
I'll hand over to someone who knows what they are doing :)


(defun ess-snarf-commands ()
   "Extract some commands from the R prompt and remove the output."
  (interactive)
  (copy-region-as-kill-nomark (mark) (point))
  (find-file "foo.Rt")
  (switch-to-buffer "foo.Rt")
  (vc-toggle-read-only)
  (yank)
  (ess-transcript-clean-buffer)
  (beginning-of-buffer-nomark)
  (end-of-buffer-mark)
  (copy-region-as-kill-nomark (mark) (point))
  (kill-buffer "foo.Rt"))



David


-- 
David Whiting, Ph.D.
Advancing Research in Chronic Disease Epidemiology (ARCHEPI) programme
Institute of Health and Society, The Medical School, 
Newcastle University, Framlington Place, Newcastle upon Tyne, NE2 4HH. 
Tel: +44 191 222 7045;  Extn: 7375; Fax: +44 191 222 8211.
http://research.ncl.ac.uk/archepi
www.ncl.ac.uk/ihs




More information about the ESS-help mailing list