Is there a way to use Rmarkdown in Spacemacs?

1.5k views Asked by At

I know that Emacs has the polymode package that allows coding in RMarkdown. However, it seems that Spacemacs is still missing the equivalent of a polymode layer.

I have been trying to install it directly into Spacemacs, with no success. Therefore my question: is there a way to edit RMarkdown files in Spacemacs (not plain Emacs).

2

There are 2 answers

2
gdkrmr On

you can add packages to spacemacs by adding them to dotspacemacs-additional-packages in your .spacemacs:

dotspacemacs-additional-packages '(polymode poly-R poly-noweb poly-markdown)

after a restart the packages should get installed automatically, you probably want to set some other options in dotspacemacs/user-config () e.g. something like:

(add-to-list 'auto-mode-alist '("\\.md" . poly-markdown-mode))
(add-to-list 'auto-mode-alist '("\\.Snw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rnw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rmd" . poly-markdown+r-mode))

Edit: polymode got a rework.

0
haff On

There's no official polymode layer for Spacemacs, but I've found a couple of implementations in random configs on GitHub. Here's one that works for me:

;;; packages.el --- polymode layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Walmes Zeviani & Fernando Mayer
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; Layer retrieved from here:
;; https://github.com/MilesMcBain/spacemacs_cfg/blob/master/private/polymode/packages.el
;;
;;; Code:

(defconst polymode-packages
  '(polymode
    poly-R
    poly-markdown))

(defun polymode/init-poly-R ())

(defun polymode/init-poly-markdown ())

(defun polymode/init-polymode ()
  (use-package polymode
    :mode (("\\.Rmd"   . Rmd-mode))
    :init
    (progn
      (defun Rmd-mode ()
        "ESS Markdown mode for Rmd files"
        (interactive)
        (require 'poly-R)
        (require 'poly-markdown)
        (R-mode)
        (poly-markdown+r-mode))
      ))
  )

;;; packages.el ends here

There are a few ways to work with private custom layers like this, but one straightforward and easy way is to...

  1. Save the code above as a file named packages.el in ~/.emacs.d/layers/private/polymode/.
  2. Add polymode to your list of dotspacemacs/layers, e.g.
(defun dotspacemacs/layers ()
  ess
  polymode
  python
  ...
  1. Restart Emacs and the polymode package should install.

Using this, you shouldn't have to use (add-to-list 'auto-mode-alist... to declare the particular mode that .Rmd files should use since it's defined in the layer. I retrieved this particular layer from here. I tried one or two others as well, but they didn't work for me.