[ESS] Automatically pasting R output into script buffer?

Vitalie Spinu spinuvit.list at gmail.com
Fri Oct 14 15:54:24 CEST 2011


Hi Aloke,

Next will do what you want. If selection is active it sends the whole
region, otherwise the current line only.


(defun ess-eval-line-or-region-in-place ()
  "Runction for evaluating a region or line (depending whether it is a
selectio ni nthe buffer."
  (interactive)
  (let ((buf (get-buffer-create " *ess-command-output*")))
    (if (not (and transient-mark-mode mark-active))
        ;; execute current line
        (ess-command (format "\n%s\n"  (buffer-substring
(point-at-bol) (point-at-eol))) buf)
      ;; else - execute region
      (ess-command (format "\n%s\n"  (buffer-substring (mark) (point))) buf)
      (setq mark-active nil)
      (goto-char (max (mark) (point)))
      )
    (with-current-buffer buf
      (goto-char (point-min))
      (while (re-search-forward "^" nil t)
        (insert "## ")))
    (set-mark (point) )
    (end-of-line)
    (insert "\n")
    (insert-buffer-substring buf)
    (forward-line)
    ))


(define-key ess-mode-map [(control ?c) (control ?j)]
'ess-eval-line-or-region-in-place)

Cheers,
Vitalie.


O Fri, Oct 14, 2011 at 11:03 AM, Aloke Phatak <Aloke.Phatak at csiro.au> wrote:
> On 14/10/2011 4:38 PM, Stephen Eglen wrote:
>>
>> Aloke Phatak<Aloke.Phatak at csiro.au>  wrote:
>>
>>> [...] I'd like to be able to execute a command from the script
>
>>> buffer (using C-c C-j, for example) and have its output pasted
>>> into my script directly below the command I've just executed.
>>>
>>> Is that possible?
>>
>> I don't think its possible with ESS as-is. [...] Would you
>
>> have the output commented?
>
> Yes, sorry, I should have been more precise, the output would be commented.
> The resulting file *could* be used as a script file, but I tend to use such
> files for documenting analyses that I've done - sort of like a text-based,
> poor-man's version of Sweave. I do use Sweave, but for quickly documenting
> an analysis for future reference or to pass on to colleagues, I prefer a
> heavily-commented transcript file.
>
>> you might want to take a look at org-babel for this.  It does more than
>> what you want, but e.g. you can have chunks like this in an R buffer:
>>
>> #+begin_src R :results output
>>   x<- rbinom(100, 10, 0.5)
>>   range(x)
>> #+end_src
>>
>> and then hit C-c C-c within that chunk; you should then get:
>> #+results:
>> : [1] 1 8
>>
>> My ESS tutorial this year gives the relevant background.
>>   http://www.damtp.cam.ac.uk/user/sje30/ess11
>
> Looks interesting already!
>
> Aloke
>
> ______________________________________________
> ESS-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help
>



More information about the ESS-help mailing list