Improved function to set default ess R session to the default Orgmode R session

This is an improved version of my prevous post Set default ess R session to the default Orgmode R session with the following improvements:

  • also works with inline R session
  • get the properties properly using (org-babel-get-src-block-info) instead of using the ugly hack I was using before.
  • check that it is an R language source block so it won’t do anything for any other languages.
  • advice applied on org-edit-special instead which works with inline source code.

In the future I need to see if this would be useful for python but at this point it is not clear if you can have multiple python session and whether you can easily swithch among them or if there a concept of a python process associated to a buffer.

(defun get-org-current-rsession()
  "When you are in an org file get the current R session based on global header, subtree, property or source code :session property whichever is relevant"
      ((mylist (org-babel-get-src-block-info))
       (prop (nth 2 mylist))
      (setq lang (nth 0 mylist))
      (when (string= "R" lang)
	(setq session_name (cdr (assq :session prop)))
	(message "R session name: %s" session_name)
	(set-other-window-func session_name)

(defun set-ess-R-process()
  "set the process to session stored in session_name variable"
  (when (string= "R" lang)
       (message "Setting R session to %s" session_name)
       (setq ess-local-process-name (process-name (get-buffer-process session_name)))

(advice-add 'org-edit-special :before #'get-org-current-rsession)
(advice-add 'org-edit-special :after #'set-ess-R-process)
Fred Gruber
Fred Gruber
Senior Principal Scientist

My research interests include causal inference, Bayesian networks, causal discovery, machine learning.