RMarkdown代码评估直到命令

时间:2016-09-28 10:39:07

标签: r knitr r-markdown

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

    是否存在一个命令,要求RMarkdown在此之前评估代码?

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

我尝试谷歌并阅读 RMarkdown 文档,但我一无所获。

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

2 个答案:

答案 0 :(得分:4)

来自knit_exit()的文档:

  

有时我们可能希望尽早退出编织过程,并完全忽略文档的其余部分。此函数提供了终止knit()的机制。

示例:

Text.

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

More text.

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

Ignored.

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

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

上面的代码产生:

enter image description here

答案 1 :(得分:0)

我无法找到在两种文档类型中获取此功能的方法。因此,如果您要创建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}

-->