Suppress Console Output in R Markdown, But Keep Plot

suppress console output in r markdown, but keep plot

Wrapping any object in invisible will prevent automatically printing it.

You should be able to use

invisible(lapply(obj,function(x) plot(x,main="some plot")))

However the fact that echo=FALSE doesn't work suggests that there might be something else going on.

Suppress output, keep pander and plot in R markdown

Using the following works:

rf_select <- var.select.rfsrc(rf_all, verbose=FALSE)
cat("\n")
pander(rf_select$varselect)

The problem is that var.select.rfsrc() uses cat() instead of message(), and somehow adding a newline is necessary since otherwise the first pander table is run together with the previous line in the markdown file.

Displaying the plots stored in a list, suppressing the names and indices from printing into console

We can use walk from purrr to display in Rmarkdown

---
title: "Title"
output: html_document

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown


```{r plot_create, echo = FALSE}

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(purrr))
p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_col()
plt_lst <- list(p1 = p1, p2 = p1, p3 = p1)
```

```{r plots, echo = FALSE, results = 'asis'}
walk(plt_lst, print)
```

-output

Sample Image


If we are trying this in R console, a for loop should also work

for(i in seq_along(plt_lst)) plt_lst[[i]]

How to show code but hide output in RMarkdown?

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

How to get console output and plot side by side in a R Notebook?

It's not perfect. For example, I didn't add in handlers for screen size. However, I can. You would have to let me know what you're looking for, though.

This uses Javascript in R Markdown. The engine for this is built-in. However, I added the code for you to check it (should you so choose): names(knitr::knit_engines$get()).

Also, in the setup chunk, I added options(width = 75). This will affect all chunk outputs. You can change this to make it a chunk-specific option. However, the default is 80, so you probably won't notice a difference. I did this because for two of the three groups, Species wrapped to the next row. However, for versicolor, it was different. This is part of the enforcement for uniformity.

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

# confirm engine for 'js' (it was #37 for me)
names(knitr::knit_engines$get())

# set the output width for chunks' render
# this is to keep the summaries even (versicolor was doing its own thing)
options(width = 75)
library(tidyverse)
```

The styles are not in a chunk. This goes between the setup and the next chunk.

<style>
.setupCols {
display:flex;
flex-direction:row;
width: 100%;
}
.setupCols p{
display:flex;
flex-direction: column;
width: 45%;
}

.setupCols pre {
display:flex;
flex-direction: column;
width: 55%
}
.setupCols pre code {
font-size: 85%;
}
</style>

Next is some code before your mapply call and code after.

<div class="setupCols">

```{r graphOne}
mapply(FUN = function(.x) {
plot(.x)
summary(.x)
}, split(iris, iris$Species), SIMPLIFY = FALSE)
```

</div>

At any point in time after this chunk, you will add a styling chunk. If you want it to apply more than once, move this chunk to the end. If you only want it to apply to the chunk (I named) graphOne, then make it the next chunk.

```{r styler,results='asis',engine='js'}

// search for class and tags
elem = document.querySelector('div.setupCols > pre > code');
// remove hashtags
elem.innerHTML = elem.innerHTML.replace(/#{2}/g, '');
// add newlines between summaries
elem.innerHTML = elem.innerHTML.replace(/\s{9}\n/g, '<br /><br />')

```

If you run this chunk inline, you will not get any output. However, you will see this output when you knit.

I also added some text here, but this is what it looks like:

Sample Image

If you wanted to see what the Javascript is doing, you could add eval = F and knit. This is what it will look like:

Sample Image

Let me know if I missed something or if you have any questions.

R: How to knit pdf and only printing output but suppress messages without `suppressMessages`?

```{r, message=FALSE}
doSomething ()
```

See https://yihui.org/knitr/options/ for more info.



Related Topics



Leave a reply



Submit