3

Polymode is very promising to handle the integration of R chunks using different modes.
I went through the documentation and it works well when I am inside .Rmd file but not inside .Rnw. In the .Rnw I found the Noweb mode activated without polymode PM-rmd. So there must be something wrong with my installation on Windows machine.

installation
I installed rmarkdown-mode from MELPA, BTW this was not shown clearly in the documentation of polymode, I wish there was a requirement section in it.

I was confused about that part of installation:

(setq load-path
      (append '("path/to/polymode/"  "path/to/polymode/modes")
              load-path))  

Because in Windows, polymode resides in c:/emacs/.emacs.d/elpa/polymode-20150105.931/ but I don't see the \modes folder in there! So is the above code needed if I had used install-packages from MELPA?

I installed the polymode package from MELPA. M-x list packages.

I have pandoc installed and checked in the PATH variables by M-x getenv RET PATH RET; pandoc was there.

relevant .init.el code

(require 'poly-R)
(require 'poly-markdown)
;; Markdown
(add-to-list 'auto-mode-alist '("\\.md" . poly-markdown-mode))

;;; R related modes
(add-to-list 'auto-mode-alist '("\\.Rnw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rmd" . poly-markdown+r-mode))
(setq ess-swv-processing-command "%s(%s)") % this to get rid of .ess_weave() function not found error  

MWE of .Rnw file

\documentclass[a4]{scrartcl}
\begin{document}
  Here is a code chunk.
<<demo, fig.height=4,message=FALSE,warning=FALSE>>=
library(ggplot2)
summary(cars)
qplot(speed,dist,data=cars) +
  geom_smooth()
@
You can also write inline expressioins, e.g. $\pi=\Sexpr{pi}$.
\end{document}

Notes

  • Windows 7 32 bit
  • Polymode updated from MELPA

Update
I used this code right after ESS code in the init.el and it worked well:

(require 'poly-R)
(require 'poly-markdown)
(add-to-list 'auto-mode-alist '("\\.Rnw" . poly-noweb+r-mode))

I realized that for MELPA installation these lines of code in the documentation are irrelevant:

(setq load-path
      (append '("path/to/polymode/"  "path/to/polymode/modes")
              load-path)) 
doctorate
  • 1,789
  • 16
  • 39

1 Answers1

3

You need to add poly-noweb+r-mode to auto-mode-alist for Rnw files. You also need to watch for conflicts with ESS. ESS adds its own mode to auto-mode-alist for Rnw files, so you have to wait until after this happens to make sure you over-ride the ESS settings. This is what I have in my .emacs:

(require 'polymode)
(require 'poly-R)
(eval-after-load 'ess-site 
  '(progn 
     (add-to-list 'auto-mode-alist '("\\.[rR]md" . poly-markdown+r-mode))
     (add-to-list 'auto-mode-alist '("\\.[rR]nw" . poly-noweb+r-mode)))
Tyler
  • 21,719
  • 1
  • 52
  • 92
  • is it necessary to put this code after ESS code in the `init.el` file or doesn't matter? – doctorate Feb 13 '15 at 21:04
  • It shouldn't matter - (eval-after-load ...) should hold off on cleaning up the ess stuff until after it has loaded. Just to be safe, I'll edit it to put the next line into the same form. – Tyler Feb 13 '15 at 21:06
  • 1
    @doctorate, @Tyler You need not put all the `(requre ..)`s, `poly-R` should be enough. Also deleting Rnw-mode is not necessary, the first assignment in `auto-mode-alist` takes precedence. – VitoshKa Feb 13 '15 at 21:16
  • Thanks @VitoshKa ! And thanks for polymode, it's fantastic – Tyler Feb 13 '15 at 21:47
  • I confirm that there is no need to `require (polymode)`. I just put the code after the ESS code and it worked well (as in the update). – doctorate Feb 14 '15 at 16:52