1

I have code like this to run R command from Emacs:

(defun call-r-command (command-list)
  "run R command in shell buffer window

  if there is displayed buffer that have shell it will use that window"
  (let* ((name "*R*")
         (new-buffer (get-buffer-create name))
         (old-buff (get-buffer name))
         (script-proc-buffer
          (apply 'make-comint-in-buffer
                 "script"
                 new-buffer
                 "c:/Program Files/R/R-3.5.2/bin/R.exe"
                 nil
                 command-list))
         (old-window (if old-buff (get-buffer-window old-buff)))
         (window (or old-window
                     (get-window-with-mode '(comint-mode eshell-mode))))
       (script-proc (get-buffer-process script-proc-buffer)))
    (if window
        (set-window-buffer window new-buffer)
      (switch-to-buffer-other-window new-buffer))))

I use this function to build R package:

(defun build-r-package ()
  (interactive)
  (call-r-command `("CMD" "INSTALL" ,(git-root-repo))))

for this I don't need to kill the process but I need this when I run process that still running (I call this function that run shiny server):

(defun shiny ()
  "run shiny R application in shell buffer"
  (interactive)
  (let ((R (concat "shiny::runApp('" default-directory "')")))
    (call-r-command `("-e" ,R "--no-save"))))

So who can I make it so when I press q it will kill the process when I'm in *R* buffer? Do I need to use set-process-sentinel function? Do I need to create minor mode to add q into *R* buffer?

I completely don't know how to make this work. Any help is appreciated. If you provide code that will make it work, it would be awesome.

2

Note that script-proc-buffer will always be new-buffer since that is an existing buffer when make-comint-in-buffer is called.

To bind kill-process to the key sequence q make it somehow interactive and use local-set-key with the wanted buffer current, e.g.:

(with-current-buffer script-proc-buffer
  (local-set-key "q" (lambda () (interactive) (kill-process))))

I would highly recommend not to use "q" as key sequence but something like (kbd "C-c q").

Applied to your example:

(defun call-r-command (command-list)
  "run R command in shell buffer window

  if there is displayed buffer that have shell it will use that window"
  (let* ((name "*R*")
         (new-buffer (get-buffer-create name))
         (old-buff (get-buffer name))
         (script-proc-buffer
          (apply 'make-comint-in-buffer
                 "script"
                 new-buffer
                 "c:/Program Files/R/R-3.5.2/bin/R.exe"
                 nil
                 command-list))
         (old-window (if old-buff (get-buffer-window old-buff)))
         (window (or old-window
                     (get-window-with-mode '(comint-mode eshell-mode))))
       (script-proc (get-buffer-process script-proc-buffer)))
    (with-current-buffer script-proc-buffer
        (local-set-key (kbd "C-c q") (lambda () (interactive) (kill-process))))
    (if window
        (set-window-buffer window new-buffer)
      (switch-to-buffer-other-window new-buffer))))

DISCLAIMER: Since I don't use R I have not tested the code. Please test and report back.

  • It works perfectly, thanks. – jcubic Apr 5 at 10:20
  • I've added small modification, I've killed previous process if there is one running for old-buffer. Because this will happen when I've run server and want to build the package when it's still running. If I didn't do this, I will have hidden process with server running or some kind of error. – jcubic Apr 5 at 10:34
  • @jcubic Do you know the ess project? I do not use it but I think they already have R-sessions. – Tobias Apr 5 at 10:45
  • I have it installed and I think I have it enable for R buffer, not sure if it have shiny (web server) support. – jcubic Apr 5 at 10:48
  • 1
    You can run a shiny app from an ESS buffer, no extra configuration is needed – Tyler Apr 5 at 15:53

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.