Putting tables automatically in tabs in Rmarkdown - r

I want to put tables produced from a for() loop in different tabs automatically. I used the code below from similar code for plots
but it doesn't work properly for tables. It creates the tabs but doesn't show the tables in them. Could anyone please correct my code?
---
output: html_document
---
# title 1 {.tabset}
There should be one table in each tab.
```{r echo=FALSE, warning=FALSE, results='asis'}
for(Species in levels(iris$Species)){
cat('\n##', Species, '\n')
p <- iris[iris$Species == Species,]
knitr::kable(p)
cat('\n')
}
```

You have to print your tables within the for loop:
---
output: html_document
---
# title 1 {.tabset}
There should be one table in each tab.
```{r echo=FALSE, warning=FALSE, results='asis'}
for(Species in levels(iris$Species)){
cat('\n##', Species, '\n')
p <- iris[iris$Species == Species,]
print(knitr::kable(p))
cat('\n')
}
```

Related

Is there a way to export multiple gt tables to a single HTML output

I have a list of many (can be dozens of) tables created with the gt package in R. I would like to export them all as a single HTML file with a little space between each table. Currently, I have been exporting each table individually, reading them into an RMarkdown with the xfun package, and knitting to a single HTML file. I'm curious if there is a method to cut out the intermediate step of saving each table individually and reading into the Rmd.
Example:
library(gt)
library(tidyverse)
tbl_list <- list(mtcar_tbl = mtcars %>% gt(),
iris_tbl = iris %>% gt(),
cars_tbl = cars %>% gt())
purrr::map(seq_along(tbl_list), function(rownum){
htmltools::save_html(html = tbl_list[[rownum]],
file = paste0("test",rownum,".html"))
})
RMarkdown to combine and export tables:
---
title: ""
output: html_document
---
```{r, echo=FALSE,message=FALSE,warning=FALSE}
library(xfun)
```
```{r echo=FALSE}
htmltools::includeHTML("test1.html")
```
<br><br>
```{r echo=FALSE}
htmltools::includeHTML("test2.html")
```
<br><br>
```{r echo=FALSE}
htmltools::includeHTML("test3.html")
```
One option would be to use purrr::walk with chunk option results="asis" to directly print your tables without the intermediate step. To add the line breaks after each table use cat("<br><br>"):
Note: For the reprex I just print the head of each table.
---
title: "Untitled"
output: html_document
date: "2022-09-19"
---
```{r echo=FALSE, results='asis', warning=FALSE, message=FALSE}
library(gt)
library(tidyverse)
tbl_list <- list(mtcar_tbl = mtcars,
iris_tbl = iris,
cars_tbl = cars)
tbl_list <- purrr::map(tbl_list, ~ head(.x) %>% gt() )
purrr::walk(tbl_list, function(x) { print(x); cat("<br><br>") })
```

Pass Params from loop to generate Dynamic Reports in Rmarkdown

Im new to Rmarkdown and I would like to create dynamic reports where every report section is generated from a template (child) document. Each section will then start with a newpage in the rendered pdf.
My approach is currently based on this post which shows how to generate dynamically text in the child (which works), however I am not able to transfer the contents of the loop into a R-Codeblock, probably because the params are not well defined in the way that I tried to do it.
This is how my parent document looks like:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blabla
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
params <- list(species = i)
}
```
`r paste(knit(text = out), collapse = '\n')`
and this is how the child looks like
---
title: "template"
output: html_document
params:
species: NA
---
# This is the reporting section of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste(params$species)
# plot doesnt work work
# plot(iris$Sepal.Length[iris$Species=={{i}}],
# iris$Sepal.Width[iris$Species=={{i}}]
# )
```
\newpage
To my understanding the parameter that is actually passed is the last species from the dataset generated in the loop but I'm not sure why the plot would't work then. Can anybody help me out on how to fix this issue?
Ok. No need to go through params. The solution was simply to put i between brackets AND parenthesis in the child-document.
Parent:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blahblah Main text before individual sections
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
}
```
`r paste(knit(text = out), collapse = '\n')`
Child
---
title: "template"
output: html_document
---
# This is the reporting page of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste("This will be a plot of Sepal Length and Witdh from", '{{i}}')
plot(iris$Sepal.Length[iris$Species=='{{i}}'],
iris$Sepal.Width[iris$Species=='{{i}}']
)
```
\newpage
Original solution found here.

R Markdown - format text as a header in a loop seems to be working for 1st loop iteration only

I am trying to generate R Markdown document with headers generated dynamically from a loop.
I used suggestions from similar SO questions: Knitr: print text from code block as R markdown, Using bold text in a loop for a Word document in Rmarkdown and wrote
---
title: "Untitled"
output:
html_document:
toc: true
toc_depth: 5
---
## R Markdown
```{r, warning=FALSE, message=FALSE, echo=FALSE}
library(ggplot2)
library(dplyr)
```
```{r fig.width=4, fig.height=2, echo=FALSE, results='asis'}
experiment_names <- paste0("Experiment ", 1:3)
for (id in 1:3){
cat(" \n")
cat("### ", experiment_names[id])
cat(" \n")
set.seed(id)
plt <-
data.frame(x = rnorm(100)) %>%
ggplot(aes(x = x)) +
geom_histogram(binwidth = 0.1)
plot(plt)
}
```
but only the 1st loop iteration header is printed correctly. How to get the two others working too?
I don't know why this works, but try one line instead of three:
cat("\n\n### ", experiment_names[id], "\n")

How to use LaTeX Code in R Chunk in R-Markdown?

I am currently writing on a report with rmarkdown and therefore I want to create sections inside a r code chunk. I figured out that this is possible with the help of cat() and results="asis". My problem with this solution is, that my R code results and code isn't properly displayed as usual.
For example
---
title: "test"
output: pdf_document
---
```{r, results='asis'}
for (i in 1:10) {
cat("\\section{Part:", i, "}")
summary(X)
$\alpha = `r X[1,i]`$
}
```
pretty much does the trick, but here there are still two problems:
the R output for summary() is displayed very strange because I guess it`s interpreted as LaTeX code
I can't use LaTeX formulas in this enviroment, so if I want every section to end with a equation, which also might use a R variable, this is not possible
Does somebody know a solution for some of these problems, or is there even a workaround to create sections within a loop and to have R code, R output and LaTeX formulas in this section? Or maybe at least one of those things?
I am very thankful for every kind of advice
You can do what you are after inline without relying as much on code blocks.
As a minimal example.
---
title: "test"
output: pdf_document
---
```{r sect1_prep, include=FALSE}
i <- 1
```
\section{`r paste0("Part: ", i)`}
```{r sect1_body}
summary(mtcars[, i])
```
$\alpha = `r mtcars[1, i]`$
```{r sect2_prep, include=FALSE}
i <- i + 1
```
\section{`r paste0("Part: ", i)`}
```{r sect2_body}
summary(mtcars[, i])
```
$\alpha = `r mtcars[1, i]`$
Produces...
If you really want to have a section factory, you could consider pander.
---
title: "test"
output: pdf_document
---
```{r setup, include=FALSE}
library(pander)
panderOptions('knitr.auto.asis', FALSE)
```
```{r, results='asis', echo=FALSE}
empty <- lapply(1:10, function(x) {
pandoc.header(paste0("Part: ", x), level = 2)
pander(summary(mtcars[, x]))
pander(paste0("$\\alpha = ", mtcars[1, x], "$\n"))
})
```
which produces...
remove summary table format example
---
title: "test"
output: pdf_document
---
```{r setup, include=FALSE}
library(pander)
panderOptions('knitr.auto.asis', FALSE)
```
```{r, results='asis', echo=FALSE}
content <- lapply(1:10, function(x) {
head <- pandoc.header.return(paste0("Part: ", x), level = 2)
body1 <- pandoc.verbatim.return(attr(summary(mtcars[, x]), "names"))
body2 <- pandoc.verbatim.return(summary(mtcars[, x]))
eqn <- pander_return(paste0("$\\alpha = ", mtcars[1, x], "$"))
return(list(head = head, body1 = body1, body2 = body2, eqn = eqn))
})
writeLines(unlist(content), sep = "\n\n")
```

How to get chunk name in knitr?

I want to set the chunk option "eval" based on a list of chunk names. Is there a function to get the chunk name in knitr, e.g. chunk_name?
This is my minimum example with fake function name "chunk_name".
---
output: html_document
---
```{r setup}
eval_chunks <- c('chunk1')
```
```{r chunk1, eval=chunk_name() %in% eval_chunks}
plot(cars)
```
```{r chunk2, eval=chunk_name() %in% eval_chunks}
plot(cars)
```
Thanks for any suggestions. Please let me know if my question is not clear.
Knitr provided labels inside a chunk since 2012 (need more Google, https://github.com/yihui/knitr/issues/73).
This is my sample Rmd file:
---
output: html_document
---
```{r setup}
library(knitr)
eval_chunks <- c('chunk1', 'chunk3')
```
```{r chunk1, eval=opts_current$get("label") %in% eval_chunks}
print(opts_current$get("label"))
```
```{r chunk2, eval=opts_current$get("label") %in% eval_chunks}
print(opts_current$get("label"))
```
```{r chunk3, eval=opts_current$get("label") %in% eval_chunks}
print(opts_current$get("label"))
```
I think this solution is imperfect because it requires a bit of care in making sure the correct chunks are evaluated, but it gets around the problem that chunk options are evaluated before hooks are called. In short, it doesn't use a hook, but instead uses the fact that chunk options can be R expressions. In this case, a function e() is used that relies on a global counter variable to dictate whether a particular chunk should be evaluated. Because chunks are evaluated in order, this works. In the below example, chunk1 and chunk3 are evaluated, but the others are not.
---
output: html_document
---
```{r setup}
library("knitr")
.i <- 2 # `setup` is the first chunk, so start at 2
.x <- all_labels() %in% c("chunk1", "chunk3")
e <- function(){
d <- .x[.i]
.i <<- .i + 1
d
}
```
```{r chunk1, eval=e()}
x <- 1
x
```
```{r chunk2, eval=e()}
x <- 2
x
```
```{r chunk3, eval=e()}
x <- 3
x
```
```{r chunk4, eval=e()}
x <- 4
x
```

Resources