suppress console output in r markdown, but keep plot
Wrapping any object in invisible
will prevent automatically print
ing 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
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:
If you wanted to see what the Javascript is doing, you could add eval = F
and knit
. This is what it will look like:
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
Creating a New Column Based on Unique Id With Values in R
Remove Last N Rows in Data Frame With the Arbitrary Number of Rows
R - Getting Characters After Symbol
How to Declare a Vector of Zeros in R
Split Data Frame String Column into Multiple Columns
Convert Continuous Numeric Values to Discrete Categories Defined by Intervals
Select/Assign to Data.Table When Variable Names Are Stored in a Character Vector
Remove Part of String After "."
How to Convert a List Consisting of Vector of Different Lengths to a Usable Data Frame in R
How to Convert a Data Frame Column to Numeric Type
Error in Confusion Matrix:The Data and Reference Factors Must Have the Same Number of Levels
Counting Unique Values Across Variables (Columns) in R
Repeat Each Row of Data.Frame the Number of Times Specified in a Column
How to Prevent Ifelse() from Turning Date Objects into Numeric Objects
Does Ifelse Really Calculate Both of Its Vectors Every Time? Is It Slow
How to Assign Colors to Categorical Variables in Ggplot2 That Have Stable Mapping