繁体   English   中英

RMarkdown / Knitr:Word 文档隐藏代码,HTML 文档代码折叠

[英]RMarkdown / Knitr: Hide code for Word document, but code folding in HTML document

对于一个项目,我希望在选择 HTML 时使用 knitr-output 来启用代码折叠,但在编写 word 文档时希望隐藏所有代码。

我当前的 RMarkdown YAML:

---
title: "Title"
author: "me"
output:
  bookdown::html_document2:
    code_folding: hide
    number_sections: false
  word_document:
    reference_docx: reference.docx
    # Option to hide code in word document?
  pdf_document:
    latex_engine: xelatex
bibliography: references.bib  
link-citations: yes
editor_options:
  markdown:
    wrap: 72
linestretch: 2
lang: en
---

word_document YAML 标签是否有一个选项可以实现这一点? 或者其他解决方法或包? 如果您在 R 块中设置echo=FALSEinclude=FALSE ,那么这也会省略 HTML 文档中的代码。 RMarkdown 文档和不同的论坛中四处查看,但找不到实现此目的的方法。

一种选择是使用knitr::is_html_output来检查您是否正在呈现为 HTML。 这可以用于有条件地为非 HTML 输出设置echo=FALSE

---
title: "Title"
author: "me"
output:
  word_document:
    # Option to hide code in word document?
    #reference_docx: reference.docx
  bookdown::html_document2:
    code_folding: hide
    number_sections: false
  pdf_document:
    latex_engine: xelatex
---

```{r include=FALSE}
if (!knitr::is_html_output()) knitr::opts_chunk$set(echo=FALSE)
```

```{r}
library(ggplot2)

ggplot(mtcars, aes(hp, mpg)) +
  geom_point()
```

文字输出

在此处输入图像描述

HTML 输出

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM