How to create a pdf from a Rmd file using Emacs, ESS, pandoc-mode and polymode?
Asked Answered
N

2

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)
```
Nanete answered 4/2, 2015 at 14:51 Comment(0)
A
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.

Amelioration answered 4/2, 2015 at 16:1 Comment(8)
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'Nanete
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.Nanete
@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.Humankind
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.Humankind
@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.Amelioration
@Amelioration thanks polymode weave is very helpful, I posted this in more detail here: emacs.stackexchange.com/a/8280/2443. You may have a look and will be glad to have your suggestions to improve my current workflow.Humankind
similar issue to sbac's - pdf not showing up when i use M-n eLur
This answer helped me, but I found that the weaver is actually named knitR-ESS.Willis
C
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.

Chidester answered 11/12, 2017 at 20:56 Comment(3)
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)Volumeter
indeed. quotes are the issue on my machine. fixed by editing one line in your code, now: (setq command (concat "echo " rcmd " | R --vanilla"))Volumeter
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.Chidester

© 2022 - 2024 — McMap. All rights reserved.