【问题标题】:RMarkdown code evaluation until commandRMarkdown 代码评估直到命令
【发布时间】:2016-09-28 10:39:06
【问题描述】:

我正在使用 RMarkdown 来构建我的分析。最终输出将是一个 html 文档。实际上,我有一个核心代码,它将成为最终文档,并且在结束之后,我有很多代码行,其中包含目前没有用的块和句子,但可能是包含在最终文档中。

    是否存在命令 RMarkdown 在此之前评估代码?

不只是像 eval=FALSE 用于块(我也有纯文本),而是像 TeX 中的 \end{document}。我不想只评论纯文本并将eval=FALSE 作为块选项。

我尝试 google 并阅读 RMarkdown 文档,但什么也没找到。

谢谢大家!并原谅我英语不好...

【问题讨论】:

  • 你可以使用knitr::opts_chunk$set(eval=FALSE) 为命令后的所有块设置 eval=FALSE ;我不知道如何轻松地为文本做到这一点。
  • 感谢@scoa。我知道chunk$set 的这种用法,实际上我用它来不评估我不需要的代码并节省时间。
  • 不完全是玩具的要求,但由于它仍会评估文本但不会在 HTML 中显示它,因此使用 HTML 注释 <!---text--->
  • @Daniel Winkler 这有点像 drmariod 回复。我更喜欢CL。解决方案,但无论如何谢谢!

标签: r knitr r-markdown


【解决方案1】:

来自knit_exit()的文档:

有时我们可能想提前退出编织过程,完全忽略文档的其余部分。该函数提供了一种终止knit()的机制。

例子:

Text.

```{r}
print(1)
```

More text.

```{r}
knitr::knit_exit()
```

Ignored.

```{r}
print("Ignored.")
```

knit_exit() 之后的所有内容都将被忽略。这适用于所有输出格式。

上面的代码产生:

【讨论】:

  • @SimoneMarini 不用担心,很高兴为您提供帮助!
【解决方案2】:

我找不到在这两种文档类型中获取此信息的方法。因此,如果您想创建 PDF,请使用第一个示例,不要使用 <!-- -->。在 HTML 中,您可以在文档中保留两个注释字符。

PDF 怎么样

title: "Untitled"
author: "Mario Dejung <m.dejung@imb.de>"
date: "28 Sep 2016"
output: pdf_document
header-includes: \usepackage{comment}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

\begin{comment}

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

\end{comment}

这适用于 HTML

---
title: "Untitled"
author: "Mario Dejung <m.dejung@imb.de>"
date: "28 Sep 2016"
output: html_document
header-includes: \usepackage{comment}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

<!--

\begin{comment}

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

\end{comment}

-->

【讨论】:

  • 不错的解决方案。我想我将来也会使用它们,但目前我正在寻找类似@CL的东西。回复。还是谢谢!
猜你喜欢
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 2011-03-25
  • 2018-05-27
  • 2018-11-05
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多