Simone Marini
Simone Marini

Reputation: 55

RMarkdown code evaluation until command

I'm using RMarkdown where I'm building my analysis. The final output will be an html document. Actually I've got a core code which will be the final document and, after the end, I've got many lines of code with chunks and sentences that at the moment are not useful, but could be included in the final document.

Not just like eval=FALSE for chunks (I've got also plain text), but something like \end{document} in TeX. I don't want just to comment plain text and put eval=FALSE as chunks options.

I tried to google and read in the RMarkdown documentation, but I found nothing.

Thanks everybody! And forgive me for my poor English...

Upvotes: 3

Views: 893

Answers (2)

CL.
CL.

Reputation: 14957

From the documentation of knit_exit():

Sometimes we may want to exit the knitting process early, and completely ignore the rest of the document. This function provides a mechanism to terminate knit().

Example:

Text.

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

More text.

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

Ignored.

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

Everything after knit_exit() will be ignored. This works for all output formats.

The code above produces:

enter image description here

Upvotes: 4

drmariod
drmariod

Reputation: 11762

I couldn't find a way to get this in both document types. So if you want to create PDFs, go for the first example and do not use <!-- -->. In HTML you can leave both comment characters inside the document.

What about this for PDF

title: "Untitled"
author: "Mario Dejung <[email protected]>"
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}

And this for HTML

---
title: "Untitled"
author: "Mario Dejung <[email protected]>"
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}

-->

Upvotes: 0

Related Questions