2

I've finally committed to moving over to both emacs and ess -- and i'm having a bit of trouble getting (the famously customisable) combination to work as i'd like.

Say that i have a file that contains an object called rainfall open in emacs and an inferior R session open. Is there a way that i can put point over the token and then send it to the R process for a function to be applied to that object?

To make it concrete, say the source file contains the following object:

rainfall <- xts(runif(100, min = 0, max = 10), seq.Date(as.Date('2019-01-01'), by = 1, length = 100))

I'd like to be able to put point anywhere inside the token rainfall and (via some keyboard shortcut) send it to the R process with some function (such as head, tail, plot, or summary) applied to it.

So in the iESS[R] screen i'd see: R> tail(rainfall) and the associated output.

1

You can achieve what you want quickly with a keyboard macro for each Emacs session.

But it looks like you would want to have several standard functions available to quickly examine R objects, so it will be more flexible to do it in Emacs Lisp.

(require 'ess-r-mode)
(defun r-summary-at-point ()
  (interactive)
  (let ((sym (ess-symbol-at-point)))
    (if sym
        (ess-send-string (get-buffer-process "*R*") 
                         (concat "summary(" (symbol-name sym) ")\n") t)
      (message "No valid R symbol at point"))))

(define-key ess-r-mode-map (kbd "C-c :") 'r-summary-at-point)

That function does not check to make sure the "*R*" process buffer exists, but will work once you have started the process, assuming the default.

| improve this answer | |
  • I tried this code, but got the error Symbol's value as variable is void: ess-r-mode-map. – ricardo May 18 at 0:14
  • Sorry - I know why - I will edit my answer. You need to require the library first. – gregoryg May 18 at 0:51
  • 1
    for some reason ess-r-mode-map broke things (even if i required it above); the good news is that this works (define-key ess-mode-map (kbd "C-c :") 'r-summary-at-point). this is probably another question, but if you have an opinion on ess-r-mode-map v. ess-mode-map i'm interested (or some links!). – ricardo May 18 at 1:40
  • ess-mode-map is more general, and probably better to use - but it's been a couple years since I used ESS on a daily basis, so take that with a grain of salt. The important thing is that you can call the function and it works - the key binding is another matter. You can always run functions with M-x eval-expression or ` a quick M-: - always try that before using a key combo! You probably already know that :-) – gregoryg May 18 at 3:53
  • 1
    I've asked a related question -- i'm building up a lot of boilerplate code in my .emacs file so i'm trying to figure out how to create a general function so that i can declare on of these in a single line. – ricardo May 18 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.