【问题标题】:Specifying multiple simultaneous output formats in knitr (new)在 knitr 中指定多个同时输出格式(新)
【发布时间】:2017-11-24 21:53:01
【问题描述】:

我可以编写一个 YAML 标头来使用 knitr 为 R Markdown 文件生成多种输出格式吗?我无法重现the original question with this title 的答案中描述的功能。

这个降价文件:

---
title: "Multiple output formats"
output: 
    pdf_document: default
    html_document:
      keep_md: yes
---

# This document should be rendered as an html file and as a pdf file

生成一个 pdf 文件,但不生成 HTML 文件。

还有这个文件:

---
title: "Multiple output formats"
output: 
  html_document:
    keep_md: yes
  pdf_document: default
---

# This document should be rendered as an html file and as a pdf file

生成一个 HTML 文件(和一个 md 文件),但不生成 pdf 文件。

后一个例子是original question 的解决方案。我曾尝试使用 Shift-Ctrl-K 和 RStudio 中的 Knit 按钮进行编织,以及调用 rmarkdown::render,但无论我使用哪种方法生成输出文件,都只创建了一种输出格式。

可能相关,但我无法确定解决方案:

使用 R 版本 3.3.1 (2016-06-21)、knitr 1.14、Rmarkdown 1.3

【问题讨论】:

  • 标题中的(new) 指的是哪里?这是knitr 的特殊版本吗,例如Windows NT 中的NT
  • @Anthon 括号中的“新”指的是这个问题与之前提出(并回答)的问题具有相同的标题。

标签: r yaml knitr r-markdown


【解决方案1】:

我其实在Render all vignette formats #1051 里简单提过,你漏掉了:

rmarkdown::render('your.Rmd', output_format = 'all')

它记录在帮助页面?rmarkdown::render

【讨论】:

  • 我可以确认这是可行的,尽管 all 所需的格式仍然需要在 YAML 标头中。如果我只包含output: html_document 或完全跳过output 规范,它只会生成一个HTML 文档。我期待 knitr 使用 YAML 标头中的所有信息,而无需我额外指定。在不尝试更改 rmarkdown::render 默认参数的情况下(不确定这是否可能?),听起来这个解决方案需要显式使用 rmarkdown::render,这意味着 Shift-Ctrl-K 快捷键仍然只会产生一种输出格式,对吧?
  • 对。你不能 Ctrl + Shift + K。
  • 你可以,使用这个答案stackoverflow.com/a/53280491/1870254
【解决方案2】:

尽管有谢一辉的权威回答,并且对伟大包的作者给予应有的尊重,但output_format = 'all'在很多情况下是次优的。
此解决方案引发的问题之一是,R 脚本是从头开始重新处理的每种格式。证明:

 rmarkdown::render("new.Rmd", output_format = c("html_document", "pdf_document"))


processing file: new.spin.Rmd
  |.......................                                               |  33%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-1
  |......................................................................| 100%
  ordinary text without R code


output file: new.knit.md

"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.html --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\fabrn\AppData\Local\Temp\RtmpW6Vban\rmarkdown-str3490247b1f1e.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" 

Output created: new.html


processing file: new.spin.Rmd
  |.......................                                               |  33%
  ordinary text without R code

  |...............................................                       |  67%
label: unnamed-chunk-1
  |......................................................................| 100%
  ordinary text without R code


output file: new.knit.md

"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.tex --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in" 

在处理大数据时,这确实是一个问题。
在实际示例中,我通常将乳胶输出用作单个 rmarkdown::render 输出,然后使用 pandoc 或类似工具(如用于 pdf 的 prince)重新处理 .tex 文件。所以我的工作流程是这样的:

 rmarkdown::render('new.R', output_format = 'latex_document')
 lapply(c("html", "pdf", ...), 
        function(form) rmarkdown::pandoc_convert("new.tex", output=paste0("new.", form)))

底线是:一切都取决于您的数据。如果很小,output_format='all' 很简单。 如果很大,最好使用通用格式(乳胶是一个不错的选择,但在某些情况下 html 可能更好)作为转换工具的输入。

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多