如何使用Emacs,ESS,pandoc-mode和polymode从Rmd文件创建pdf?

sba*_*bac 16 emacs ess knitr r-markdown

这是对"经典"Rmd文件的改编,我希望使用Emacs(Emacs Speak Statistics)和多态编码将其编织为pdf.我无法找到正确的命令来做到这一点.关于polymode的文档很少.我正在使用社交科学的Emacs入门套件.

---
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)
```
Run Code Online (Sandbox Code Playgroud)

Vit*_*hKa 9

正如文档所说,使用 M-n w和M-n W设置/改变编织者.使用ESS,您可能应该使用knitr-ESSweaver,因为它使用当前*R*进程.


Ste*_*vey 5

您可以使用rmarkdown::render()rmarkdown包将文件编织.Rmd为 markdown 并使用单个命令渲染输出文件(PDF、Word、HTML 等)!

我不确定rmarkdownESS 中是否已经包含对工作流程的支持(并且我正在尝试涉足 elisp),因此我编写了一个函数,该函数调用rmarkdown::render()并允许使用rmarkdown::render()前缀 arg(例如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)
Run Code Online (Sandbox Code Playgroud)

请注意,我有一些特定的参数设置,例如output_dir = '../reports'但可以轻松自定义 elisp 以满足您的需求。

C-c r在您的初始化文件中,您只需从文件内部输入.Rmd(或C-u C-c r渲染为不同的格式、位置等)。该命令将打开一个新窗口,其中包含一个名为“错误”的缓冲区*compilation*。

这绝对可以改进,我很想听听建议。