Emacs, R, Org-mode: how to enable automatic switch to ESS-mode within R code blocks?
Asked Answered
E

5

11

When using R code chunks in an .Rnw document, the emacs buffer automatically detects that the cursor is within a code chunk (i.e. within <<>> and @), and switches to ESS-mode -- this is very valuable because one can get ESS-based code-formatting within the code-chunk, and more-usefully, one can send code lines/regions to the inferior *R* process-buffer.

How do I get the same functionality in an org-mode file within R code blocks (i.e. between #+begin_src R and #+end_src) -- I'd like emacs to automatically recognize it's within an R code code block, and turn on ESS-mode so I can send snippets of code to the *R* process. I am aware that I can do Ctl-C ' and switch to a different ESS-mode buffer where I can edit the code and get all the other ESS-mode conveniences (including sending code to R). However I'd like to not have to do this, i.e. I want to be able to send code-snippets from the R code block in the same org-mode buffer.

Eastward answered 5/12, 2011 at 14:37 Comment(2)
There's a mmm-mode - may be it can help You. I never used it myself.Leschen
Answers to this question are now 8 years old, but the question is still very relevant. Org-mode has been evolving very fast. It would be nice if more up-to-date answers were posted.Sewan
S
4

A year ago or so, I asked the same question on the org-mode-list. @cm2 has already mentioned, that it is not possible to use ESS functionality within org-mode; as far as I know Emacs cannot handle two major modes and even with mmm-mode there seem to be some difficulties.

Dan Davison kindly posted some elisp-code which mimics some of the ESS features. So, you might want to check Dan's replies to my question.

Scratches answered 5/12, 2011 at 23:24 Comment(1)
Thank you @Bernd. The email thread you mention does seem to address most of the functionality I needed. I have to investigate how to enable "shift-enter" to send code from the R code block to the R process. Then I'll be all set. I'm selecting yours as the accepted answer, since it pointed to an actual email thread with a nearly complete solution.Eastward
C
5

AFAIK, this can not be done with the current implementation of Org-mode.

I'm not sure this is doable at all within Org-mode without some delving into elisp code. The main point of the C-c ' command in Org-mode is so that you have an additional buffer that has all the syntax highlighting that you want/need for your particular language.

There is probably a way to hack around this for R-specific code by writing some Org-mode hook that checks first to see if you are in an #+begin_src R ...#+end_src R block, and then checking for a code chunk. I suspect this would be messy and conflict with other Org-mode features. In addition, you would have to write another function to grab the current code block and pass it to Org-mode's R interpreter, along with all the options you added to the src block.

Cahier answered 5/12, 2011 at 17:45 Comment(1)
Thanks for responding, accepting yours as the best answer. It's good to know that there's no solution out there that I'm missing. And for now I'll give up on it, and include Ctl-C ' in my workflow.Eastward
S
4

A year ago or so, I asked the same question on the org-mode-list. @cm2 has already mentioned, that it is not possible to use ESS functionality within org-mode; as far as I know Emacs cannot handle two major modes and even with mmm-mode there seem to be some difficulties.

Dan Davison kindly posted some elisp-code which mimics some of the ESS features. So, you might want to check Dan's replies to my question.

Scratches answered 5/12, 2011 at 23:24 Comment(1)
Thank you @Bernd. The email thread you mention does seem to address most of the functionality I needed. I have to investigate how to enable "shift-enter" to send code from the R code block to the R process. Then I'll be all set. I'm selecting yours as the accepted answer, since it pointed to an actual email thread with a nearly complete solution.Eastward
I
0

Could be that the new polymode mode https://github.com/vitoshka/polymode/blob/master/readme.md could help. I have not tried yet, but seems promising.

Isotope answered 8/7, 2014 at 22:52 Comment(0)
P
0

The poly-org.el in polymode package, is designed to address multiple mode editing in org-mode. I tried, but it freezes Emacs due to not enabling jit-lock in indirect buffer.

Profession answered 26/11, 2014 at 20:56 Comment(0)
Z
0

A workaround is toggling manually between modes when needed, for instance, adding the following code to .emacs and using F6.

;; Toggle between org & R modes.
(defun toggle-org-R-mode ()
  (interactive)
  "Toggle mode between org-R modes"
  (cond 
   ((string= major-mode "org-mode")
    (R-mode))
   ((string= major-mode "ess-mode")
    (org-mode))
   )
  )
(global-set-key [f6] 'toggle-org-R-mode)
Zedoary answered 6/3, 2015 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.