5

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).

Louis15
  • 295
  • 3
  • 12

2 Answers2

3

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.

gdkrmr
  • 674
  • 4
  • 16
  • Thanks for your answer. So, and how would I them compile it to a PDF to test if it is indeed working? Because I followed your suggestion but nothing happened. – Louis15 Sep 25 '17 at 21:12
  • There is no layer for polymode in spacemacs, you will have to use the standard keybindings of polymode, the prefix should be `M-n`, see here: https://github.com/vspinu/polymode#basic-usage – gdkrmr Sep 26 '17 at 17:56
2

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.

haff
  • 918
  • 2
  • 9
  • 20