首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Emacs、ESS、pandoc模式和多模式从Rmd文件中创建pdf?

如何使用Emacs、ESS、pandoc模式和多模式从Rmd文件中创建pdf?
EN

Stack Overflow用户
提问于 2015-02-04 14:51:07
回答 1查看 4.2K关注 0票数 17

这是一个“经典的”Rmd文件的改编,我想编织成一个pdf使用Emacs ()和多模式。我找不到正确的命令来做到这一点。几乎没有关于多模的文档。我正在为社会科学使用Emacs初学者工具包。

代码语言:javascript
复制
---
title: "Untitled"
author: "SB"
date: "Wednesday, February 04, 2015"
output: pdf_document
---

You can embed an R code chunk like this:

```{r}

摘要(Cars)

代码语言:javascript
复制
You can also embed plots, for example:

```{r, echo=FALSE}

阴谋(汽车)

代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

发布于 2017-12-11 20:56:08

您可以使用rmarkdown::render()rmarkdown包编织一个.Rmd文件来标记并呈现输出文件(PDF、Word、HTML等)。只有一个命令!

我不确定ESS中是否已经包含了对rmarkdown工作流的支持(我正在尝试使用elisp),因此我编写了一个函数,它调用rmarkdown::render()并允许使用前缀arg (例如,C-u)定制rmarkdown::render()函数调用的输入。

代码语言:javascript
复制
;; 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',但是可以很容易地定制这个参数以满足您的需要。

在init文件中这样做,只需要在C-c r文件中输入.Rmd (或将C-u C-c r呈现为不同的格式、位置等)。该命令将使用名为*compilation*的缓冲区打开一个新窗口,在该窗口中将出现任何错误。

这肯定是可以改进的,我很想听听你的建议。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28324288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档