How to embed plots into a tab in RMarkdown in a procedural fashion?
I played around a little and found a solution. You have to use print
within the asis
code chunk...
```{r}
library(ggplot2)
gg0 <- list()
gg0[[1]] <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
gg0[[2]] <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
gg0[[3]] <- ggplot(mtcars, aes(mpg, drat)) + geom_point()
headings <- c('hp','disp','drat')
```
#### Heading {.tabset}
```{r, results='asis', echo = FALSE}
for (i in 1:length(gg0)) {
cat("##### ",headings[i],"\n")
print(gg0[[i]])
cat('\n\n')
}
```
As an explanation, the cat
command together with results='asis'
produces the markdown code for a lower level headline and prints the ggplot graph afterwards. Since we used `{.tabset} in the parent headline, it creates the plots in separate tabs.
Dynamically control number of tabsets in R Markdown
If I add an extra newline at the end of each section, I get the desired results:
```{r, results='asis'}
headers <- list('graph 1', 'graph 2', 'graph 3')
for (h in headers){
cat("##", h, '<br>', '\n')
cat('This is text for', h, '<br>')
plot.new()
plot(diffinv(rnorm(100)), type = 'o', main = h)
cat('\n', '<br>', '\n\n')
}
```
Remember that Markdown often requires a full blank line between different elements.
Adjust .tabset panel html output length R
In RMarkdown we can control the size of plots using fig.dim
fig. width
or fig.height
(see here).
In your case setting fig.height
to 10
yields good results:
---
title: "test"
output: html_document
date: "2022-07-27"
---
```{r}
library(ggplot2)
library(patchwork)
gg0 <- list()
p1 <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p3 <- ggplot(mtcars, aes(mpg, drat)) + geom_point()
gg0[[1]] <- p1/p1/p1/p1
gg0[[2]] <- p1/p1/p1/p1/p1/p1
gg0[[3]] <- p1/p1/p1/p1/p1/p1/p1/p1/p1
headings <- c('hp','disp','drat')
```
#### Heading {.tabset}
```{r, results='asis', echo = FALSE, fig.height = 10}
for (i in 1:length(gg0)) {
cat("##### ",headings[i],"\n")
print(gg0[[i]])
cat('\n\n')
}
```
Related Topics
Counting the Number of Values Greater Than 0 in R in Multiple Columns
R: Split String into Numeric and Return the Mean as a New Column in a Data Frame
Select N Rows Above and Below Match
Take the Subsets of a Data.Frame with the Same Feature and Select a Single Row from Each Subset
How to Unlock Environment in R
Change Value to Percentage of Row in R
Wordcloud Package: Get "Error in Strwidth(…):Invalid 'Cex' Value"
Unexpected Date When Converting Posixct Date-Time to Date - Timezone Issue
Obtaining Twitter Screen Names from a Twitter List
How to Get a Minimum Value by Group
Convert Numeric Vector to Binary (0/1) Based on Limit
Automatically Generate New Variable Names Using Dplyr Mutate
Stargazer Output Appears Below Text - Rmarkdown to PDF
Ggplot2: Adding Lines in a Loop and Retaining Colour Mappings
Download .Rdata and .CSV Files from Ftp Using Rcurl (Or Any Other Method)
Add a Series of Elements in Different Locations Within a Vector