Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
```
like image 813
sbac Avatar asked Feb 04 '15 14:02

sbac


2 Answers

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.

like image 152
VitoshKa Avatar answered Dec 02 '22 03:12

VitoshKa


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.

like image 27
Stefan Avey Avatar answered Dec 02 '22 02:12

Stefan Avey