3
votes

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.

    Does a command exist to say to RMarkdown to evaluate the code until that point?

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...

2
you could use knitr::opts_chunk$set(eval=FALSE) to set eval=FALSE for all chunks after the command ; I have no idea how to do that easily for the text.scoa
Thank's @scoa. I know this use of chunk$set, and in fact I use it to not evaluate my code not needed and save time.Simone Marini
Not exactly what toy are asking for but since it will still evaluate the text but not show it in HTML is using HTML comment <!---text--->Daniel Winkler
@Daniel Winkler this is something like drmariod reply. I prefer CL. solution, but thanks anyway!Simone Marini

2 Answers

4
votes

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

0
votes

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}

-->