Skip to content

Instantly share code, notes, and snippets.

@kjhealy
Created October 13, 2009 01:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjhealy/208908 to your computer and use it in GitHub Desktop.
Save kjhealy/208908 to your computer and use it in GitHub Desktop.
;; ESS: Emacs Speaks Statistics
(load "~/elisp/vendor/ess/lisp/ess-site.el")
;; Use shift-enter to split window & launch R (if not running), execute highlighted
;; region (if R running & area highlighted), or execute current line
;; (and move to next line, skipping comments). Nice.
;; See http://www.emacswiki.org/emacs/EmacsSpeaksStatistics,
;; FelipeCsaszar. Adapted to spilit vertically instead of
;; horizontally.
(setq ess-ask-for-ess-directory nil)
(setq ess-local-process-name "R")
(setq ansi-color-for-comint-mode 'filter)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
(defun my-ess-start-R ()
(interactive)
(if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
(progn
(delete-other-windows)
(setq w1 (selected-window))
(setq w1name (buffer-name))
(setq w2 (split-window w1 nil t))
(R)
(set-window-buffer w2 "*R*")
(set-window-buffer w1 w1name))))
(defun my-ess-eval ()
(interactive)
(my-ess-start-R)
(if (and transient-mark-mode mark-active)
(call-interactively 'ess-eval-region)
(call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key [(shift return)] 'my-ess-eval)))
(add-hook 'inferior-ess-mode-hook
'(lambda()
(local-set-key [C-up] 'comint-previous-input)
(local-set-key [C-down] 'comint-next-input)))
(require 'ess-site)
@daroczig
Copy link

Great stuff, thanks!

However after adding this to my .emacs and trying to eval a line/region without a running R session (so shift-return starts it) I get the following error msg:

> > assignInNamespace(".help.ESS",help, ns = asNamespace("base"))
> options(STERM='iESS', str.dendrogram.last ="'", editor='emacsclient')
> > 
Error: unexpected '>' in ">"

Do you have any ideas how to resolve that? When I start an R process before, it works like a charm and assignInNameSpace is run fine.

@tmalsburg
Copy link

I had the same problem. The culprit is where the window is split and R is started (lines 24, 25). The code starts the R interpreter in the wrong window. At first this is not a problem but later when the current line is executed (line 32, 33), the incorrect window has focus and the wrong line is executed. My version of my-ess-start-R looks like this:

(defun my-ess-start-R ()
  (interactive)
  (if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
      (progn
        (setq cur-window (selected-window))
        (print cur-window)
        (case (length (window-list))
          (1 (select-window (split-window-right)))
          (t (other-window 1)))
        (R)
        (print "R started")
        (select-window cur-window))))

This code does something slightly different than the version above. If there's only one window, it is split and R is started in the right window (the new one). If there are several windows, R is started in one of them (but not in the window that contains the code). This means that my version is more conservative and doesn't unnecessarily mess with the layout of the windows. Which behavior you prefer is a matter of taste, of course. Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment