10
votes

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.

5
There's a mmm-mode - may be it can help You. I never used it myself.Adobe

5 Answers

4
votes

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.

5
votes

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.

0
votes

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.

0
votes

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.

0
votes

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)