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

时间:2015-02-04 14:51:07

标签: emacs knitr r-markdown ess

这是对“经典”Rmd文件的改编,我希望使用Emacs( Emacs Speak Statistics )和alphaode 编织为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)
```

2 个答案:

答案 0 :(得分:8)

doc says使用 M-n w M-n W 来设置/改变编织者。使用ESS,您可能应该使用knitr-ESS weaver,因为它使用当前的*R*进程。

答案 1 :(得分:3)

您可以使用rmarkdown package中的rmarkdown::render()编织.Rmd文件进行降价,并使用一个命令渲染输出文件(PDF,Word,HTML等)!< / p>

我不确定是否已经在ESS中包含对rmarkdown工作流的支持(我正试图涉及elisp)所以我编写了一个调用rmarkdown::render()的函数并允许自定义使用前缀arg(例如rmarkdown::render())输入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)

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

在您的init文件中使用此选项时,您只需从C-c r文件(或.Rmd)内输入C-u C-c r即可呈现为其他格式,位置等。该命令将打开一个新窗口,其中包含一个名为*compilation*的缓冲区,其中将显示任何错误。

这绝对可以改善,我很乐意听到建议。