17

This is an adaptation of a "classic" Rmd file that I want to knit as a pdf using Emacs (Emacs Speak Statistics) and polymode. I can't find the right commands to do that. There is little documentation about polymode. I am using Emacs Starter Kit for the Social Sciences.

---
title: "Untitled"
author: "SB"
date: "Wednesday, February 04, 2015"
output: pdf_document
---

You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```
sbac
  • 1,897
  • 1
  • 18
  • 31

2 Answers2

11

As the doc says use M-n w and M-n W to set/change the weaver. With ESS you probably should use knitr-ESS weaver as it uses current *R* process.

VitoshKa
  • 8,387
  • 3
  • 35
  • 59
  • 1
    The file is _sb_ex.Rmd_ . I used M-n W to chose knitr-ESS and then M-n w but the result is an error: `> library(knitr); knit('sb_ex.Rmd', output='sb_ex[weaved].md') Error in library(knitr) : there is no package called 'knitr'` – sbac Feb 04 '15 at 18:53
  • 1
    It didn't work in Windows but _worked in Mac OSX_. The only problem is that when I try to use `M-n e` to use _pandoc_ to export for pdf I can not see pdf format as an option. I then use _pandoc-mode_ to create a pdf in Emacs menu. – sbac Feb 04 '15 at 19:32
  • @sbac, `there is no package called \`knitr\`` indicates that you should install this package, in ESS `C-c C-e i` install it and then `library(knitr)` and u are set. – doctorate Feb 13 '15 at 15:54
  • I have a similar problem I still don't get a PDF or HTML. Pandoc is displayed among choices but no output. If it worked with you can you pls update your post with more explanation about this workflow, it must be useful for others as well. – doctorate Feb 13 '15 at 16:04
  • 1
    @doctorate, you need your pandoc installed for this to work. If it doesn't work go to `*polymode weave*` buffer. It should display the pandoc error if any. – VitoshKa Feb 15 '15 at 10:42
  • @VitoshKa thanks *polymode weave* is very helpful, I posted this in more detail here: http://emacs.stackexchange.com/a/8280/2443. You may have a look and will be glad to have your suggestions to improve my current workflow. – doctorate Feb 16 '15 at 11:21
  • similar issue to sbac's - pdf not showing up when i use M-n e – ilya May 30 '15 at 21:13
  • This answer helped me, but I found that the weaver is actually named ```knitR-ESS```. – ericOss Mar 27 '19 at 07:18
5

You can use rmarkdown::render() from the rmarkdown package to knit an .Rmd file to markdown and render the output file (PDF, Word, HTML, etc.) with a single command!

I wasn't sure if support for an rmarkdown workflow was already included in ESS (and I'm trying to dabble in elisp) so I wrote a function that calls rmarkdown::render() and allows customizing the inputs to rmarkdown::render() function call with a prefix arg (e.g., C-u).

;; spa/rmd-render
;; Global history list allows Emacs to "remember" the last
;; render commands and propose as suggestions in the minibuffer.
(defvar rmd-render-history nil "History list for spa/rmd-render.")
(defun spa/rmd-render (arg)
  "Render the current Rmd file to PDF output.
   With a prefix arg, edit the R command in the minibuffer"
  (interactive "P")
  ;; Build the default R render command
  (setq rcmd (concat "rmarkdown::render('" buffer-file-name "',"
                 "output_dir = '../reports',"
                 "output_format = 'pdf_document')"))
  ;; Check for prefix argument
  (if arg
      (progn
    ;; Use last command as the default (if non-nil)
    (setq prev-history (car rmd-render-history))
    (if prev-history
        (setq rcmd prev-history)
      nil)
    ;; Allow the user to modify rcmd
    (setq rcmd
          (read-from-minibuffer "Run: " rcmd nil nil 'rmd-render-history))
    )
    ;; With no prefix arg, add default rcmd to history
    (setq rmd-render-history (add-to-history 'rmd-render-history rcmd)))
  ;; Build and evaluate the shell command
  (setq command (concat "echo \"" rcmd "\" | R --vanilla"))
  (compile command))
(define-key polymode-mode-map (kbd "C-c r")  'spa/rmd-render)

Note that I have some specific parameter settings like output_dir = '../reports' but the elisp can be easily customized to suit your needs.

With this in your init file, you only need to enter C-c r from inside your .Rmd file (or C-u C-c r to render to a different format, location, etc.). The command will open a new window with a buffer called *compilation* where any errors will appear.

This could definitely be improved and I'd love to hear suggestions.

Stefan Avey
  • 1,148
  • 12
  • 23
  • So I love this concept, but it's not working for me, and I'm a lousy elisp programmer. It looks to me like the issue is that the command is being sent to R in double-quotes, so it's being interpreted by R as a string literal. (I'm on Windows) – mac Dec 03 '18 at 16:22
  • indeed. quotes are the issue on my machine. fixed by editing one line in your code, now: `(setq command (concat "echo " rcmd " | R --vanilla"))` – mac Dec 03 '18 at 16:31
  • 1
    For future readers, it is simpler to use polymode's built-in functionality if you don't need many customizations. `M-n E` to choose exporter and `M-n e` to export r markdown document to desired format. – Stefan Avey Feb 07 '20 at 21:01