How can I use Emacs ESS mode with R markdown?
Asked Answered
C

3

59

Is there a way to configure Emacs so that it recognizes (and allows execution of) R code chunks within R markdown (.Rmd) files similar to the way it recognizes code chunks in .Rnw (Sweave) or .org (org-mode) files?

Cherey answered 23/4, 2013 at 14:44 Comment(0)
S
22

The other answers are rather outdated, and polymode now provides a specialized package for working with .Rmd files. Simply use the poly-R package from MELPA which has its source here on Github.

The following may be added to your init.el or where-ever you use use-package:

(use-package poly-R
  :ensure t
  :pin melpa-stable)

For a more detailed use-case involving modifying the default keymaps see this configuration. The documentation describes the default keymaps.

Siloa answered 15/1, 2020 at 11:38 Comment(1)
This worked. I installed the ess and poly-R packages with MELPA and (for my Mac) I needed to modify my emacs exec-path to make it all workDoug
I
37

polymode is being developed which does/will allow you to write markdown with R chunks (for processing with knitr).

Instructions for setting up polymode+rmarkdown are in the README

It is not yet done, but is progressing. See also this discussion on the ess-help mailing list (gmane archive|mailing list archive)


Update: polymode has been released on MELPA, simplifying the installation process by using emacs' existing package mechanism. See the announcement on the ESS mailing list here.

Isochroous answered 23/4, 2013 at 18:2 Comment(0)
P
23

Download the zip for polymode from github.com/vitoshka/polymode or, if you are a "git guy", you might use:

git clone https://github.com/vitoshka/polymode.git

Add this function to your Emacs init file:

(defun rmd-mode ()
  "ESS Markdown mode for rmd files"
  (interactive)
  (setq load-path 
    (append (list "path/to/polymode/" "path/to/polymode/modes/")
        load-path))
  (require 'poly-R)
  (require 'poly-markdown)     
  (poly-markdown+r-mode))

Now, if you already have ESS installed, just open your rmd file and type Meta-Xrmd-mode and watch the beauty of Emacs.

Update for MELPA users

polymode from MELPA uses a flat file structure. When you install from an Emacs repository via M-x list-packages or like facilities, you get your package files straight where they ought to, i.e.: (expand-file-name "~/.emacs.d/elpa"), therefore do comment the above load-path code:

;;  (setq load-path 
;;      (append (list "path/to/polymode/" "path/to/polymode/modes/")
;;          load-path))
Phalan answered 27/4, 2014 at 17:3 Comment(6)
Thanks very much for this code. I want only mention that this works only if markdown-mode.el is in the "path/to/" folder (see link).Journalistic
when I download polymode from MELPA it goes into this path on my windows machine c:/emacs/.emacs.d/elpa/polymode-20150105.931/ but I don't see the modes folder in that! any idea? can you refere to windows in your answer, very much appreciate that.Corson
how do I execute R code with this setup? I don't see ESS menu for switching between Rmd and R buffer, executing code, etcMahon
@pavopax: ESS needs a separate setup. Once done, M-x R starts R and inside a chunk C-c C-z switches to R console.Phalan
the key is "inside R chunk" not just anywhere in buffer. thanks!Mahon
Polymode from git uses a flat structure too.Cleo
S
22

The other answers are rather outdated, and polymode now provides a specialized package for working with .Rmd files. Simply use the poly-R package from MELPA which has its source here on Github.

The following may be added to your init.el or where-ever you use use-package:

(use-package poly-R
  :ensure t
  :pin melpa-stable)

For a more detailed use-case involving modifying the default keymaps see this configuration. The documentation describes the default keymaps.

Siloa answered 15/1, 2020 at 11:38 Comment(1)
This worked. I installed the ess and poly-R packages with MELPA and (for my Mac) I needed to modify my emacs exec-path to make it all workDoug

© 2022 - 2024 — McMap. All rights reserved.