[r] How to show code but hide output in RMarkdown?

I want my html file to show the code, but not the output of this chunk:

```{r echo=True, include=FALSE}
fun <- function(b)
    {
    for(a in b)
        {print(a)
        return(a * a)}
    }
y <- fun(b)
```

When I run the code, i need the print to see the progress (it is quite a long function in reality).

But in the knitr file, I use the output in a further chunk, so I do not want to see it in this one (and there's no notion of progress, since the code has already been run).

This echo=True, include=FALSE here does not work: the whole thing is hidden (which is the normal behavior of include=FALSE).

What are the parameters I could use to hide the prints, but show my code?

This question is related to r knitr r-markdown

The answer is


As @ J_F answered in the comments, using {r echo = T, results = 'hide'}.

I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!

You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.

Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
                      results = "hide")
```

Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.

```{r analysis, results="markup"}
# code here
```

For what it's worth.

```{r eval=FALSE}

The document will display the code by default but will prevent the code block from being executed, and thus will also not display any results.


For completely silencing the output, here what works for me

```{r error=FALSE, warning=FALSE, message=FALSE}
invisible({capture.output({


# Your code here
2 * 2
# etc etc


})})
```

The 5 measures used above are

  1. error = FALSE
  2. warning = FALSE
  3. message = FALSE
  4. invisible()
  5. capture.output()

For muting library("name_of_library") codes, meanly just showing the codes, {r loadlib, echo=T, results='hide', message=F, warning=F} is great. And imho a better way than library(package, warn.conflicts=F, quietly=T)


The results = 'hide' option doesn't prevent other messages to be printed. To hide them, the following options are useful:

  • {r, error=FALSE}
  • {r, warning=FALSE}
  • {r, message=FALSE}

In every case, the corresponding warning, error or message will be printed to the console instead.


To hide warnings, you can also do {r, warning=FALSE}


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to knitr

How to show code but hide output in RMarkdown? R Markdown - changing font size and font type in html output Plot size and resolution with R markdown, knitr, pandoc, beamer How to set size for local image using knitr for markdown? Hiding the R code in Rmarkdown/knit and just showing the results R - Markdown avoiding package loading messages How to convert R Markdown to PDF?

Examples related to r-markdown

How to show code but hide output in RMarkdown? How to add new line in Markdown presentation? R Markdown - changing font size and font type in html output How to add \newpage in Rmarkdown in a smart way? Insert picture/table in R Markdown Centering image and text in R Markdown for a PDF report Plot size and resolution with R markdown, knitr, pandoc, beamer Hiding the R code in Rmarkdown/knit and just showing the results R - Markdown avoiding package loading messages How to convert R Markdown to PDF?