115

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?

Laurent
  • 1,914
  • 2
  • 11
  • 25
  • 74
    Try `{r echo=T, results='hide'}` – J_F Dec 08 '17 at 08:39
  • 1
    Nice, that's exactly what I was looking for! I hadn't seen that `results` parameter before – Laurent Dec 08 '17 at 08:42
  • 1
    But how would one hide _any and all_ output? Using `results='hide'` will still show things like warnings, like when an object is masked when using `library(package)`. Is there an option to hide everything that would be printed to the console? – stragu Jun 21 '18 at 12:56
  • 4
    You can do that with `warn.conflicts=F, quietly=T` in your `library` command, like `library(package, warn.conflicts=F, quietly=T)` – Laurent Jun 22 '18 at 13:26
  • 2
    I have no idea why but this does not work for me – T.Omalley Apr 30 '20 at 12:52

6 Answers6

108

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
```
Nova
  • 5,423
  • 2
  • 42
  • 62
  • really nice. I find it very helpful when doing sudo codes or just creating a notebook with functioning code(that I can test) but excluding the output so that I can keep it short and tidy! Thanks. – Meisam H Jun 13 '21 at 17:10
60

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.

jmon12
  • 1,090
  • 8
  • 17
28
```{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.

DryLabRebel
  • 8,923
  • 3
  • 18
  • 24
  • 2
    For me, I wanted to show the code and not the plot so I used `echo=TRUE, eval=FALSE` – GISHuman Apr 01 '20 at 22:21
  • that's much better. For me `result="hide"` was only showing the last line of the code, `eval=F` showed the whole thing. – Salix Jun 27 '20 at 18:05
  • 2
    `eval=F` only is good if you don't have to use the result of this chunk into the next ones. – igorkf Oct 20 '20 at 19:17
  • I agreed with @igorkf, `eval=F` is not suitable if that chunk is the chunk used for the next ones. Use `echo=T, results='hide'` instead. – Zawir Amin Jul 02 '21 at 16:09
  • @igorkf - Yes I also agree. eval=F will not run the code block. So any results won't be available for the remainder of the script. These options all have use cases. – DryLabRebel Jul 04 '21 at 04:21
5

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)

alika
  • 419
  • 5
  • 7
  • `library(package, warn.conflicts=F, quietly=T)` suppresses messages in the console but not in the HTML output when Knitr knits to HTML. – randy May 09 '21 at 23:30
5

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()
stevec
  • 41,291
  • 27
  • 223
  • 311
4

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