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.
Related
How to use code chunk from child document in another Rmd file? I have one parent document named figures.Rmd which have following structure
`{r child='force.Rmd',echo=F,ref.label='pressure'}
`
some text
`{r child='force.Rmd',echo=F,ref.label='cars'}
and the child document named force.Rmd which have following structure
`{r, cars,echo=F}
options(knitr.duplicate.label = 'allow')
plot(cars)
`
`{r, pressure,echo=F}
options(knitr.duplicate.label = 'allow')
summary(pressure)
`
I want to read some chunks from the force.Rmd in the figures.Rmd. I am getting some output but it is producing the output 2 times. So basically I have 2 or 3 chunks in force.Rmd which I want to use at different places in figures.Rmd . How can I do that without getting the output 2 times?
This is parent.Rmd:
---
title: "parent"
author: "Me"
date: "2023-01-25"
output: html_document
---
## Parent
This is parent
## Some part of child
```{r echo=FALSE}
library(knitr)
invisible(knitr::purl("child.Rmd", output="temp", quiet=TRUE))
read_chunk("temp")
```
```{r ref.label='cars'}
```
## Some text
Here I am.
## Some other part of child
## Some part of child
```{r ref.label='pressure'}
```
```{r echo=FALSE}
unlink("temp")
```
This is child.Rmd
```{r cars}
summary(cars)
```
```{r pressure}
plot(pressure)
```
I can call each single child chunk at various positions:
I am making my code more modular and would like to run multiple RMarkdown files from one overall RMarkdown. I believe I could do this if I translated all my RMarkdown files to .R scripts and used source(), but I like the document-like nature of RMarkdown and I can describe what I'm doing as I'm doing it in plain text.
The goal is to wrangle data and export a usable .sav file. I want to run clean.rmd from run.rmd, but I don't want any HTML/pdf/etc. output. Removing the output line in the YAML header doesn't prevent output. If there is a way to do this without translating everything to .R scripts, I would be very appreciative. Thank you.
clean.rmd: Script that does the cleaning
---
title: "clean"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
library(tidyverse)
library(haven)
```
```{r}
data(cars)
cars <- cars %>%
mutate(newvar = speed + dist)
```
```{r}
write_spss(cars, "~/Documents/cars_new.sav", compress = FALSE)
```
run.rmd: Script that runs clean.rmd
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
rmarkdown::render("~/Documents/clean.rmd")
```
Thank you for your help! This function works:
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
#output: html_document
---
```{r}
source_rmd = function(file, ...) {
tmp_file = tempfile(fileext=".R")
on.exit(unlink(tmp_file), add = TRUE)
knitr::purl(file, output=tmp_file)
source(file = tmp_file, ...)
}
```
```{r}
source_rmd("~/Documents/clean.rmd")
```
I use this rmd:
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
---
## A function that generates sections
```{r setup, include = FALSE}
library(pander)
create_section <- function() {
# Inserts "## Title (auto)"
pander::pandoc.header('Title (auto)', level = 2)
# Section contents
# e.g. a random plot
plot(sample(1000, 10))
# a list, formatted as Markdown
# adding also empty lines, to be sure that this is valid Markdown
pander::pandoc.p('')
pander::pandoc.list(letters[1:3])
pander::pandoc.p('')
}
```
## Generate sections
```{r, results='asis', echo=FALSE}
n_sections <- 3
for (i in seq(n_sections)) {
create_section()
}
```
and then:
library(knitr);
library(rmarkdown);
setwd("C:/bla")
knit('test_md.Rmd')
rmarkdown::pandoc_convert("test_md.md", to = "pdf", output = "test_pdf.pdf")
This kind of works but the plots are all rendered after the sections:
Each section should contain the plot. Any ideas? Thanks!
PS:
Wrapping:
plot(sample(1000, 10))
in print:
print(plot(sample(1000, 10)))
forces output to be produced. Unfortunately, NULL is also printed underneath the plot.
Could you just add pdf_document in the YAML and then generate the PDF by knitting?
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
output: pdf_document
---
I was able to get the same result you described when running the rmarkdown::pandoc_convert() on the .md file, all the plots at the end of the .PDF file.
I want to add the table of content in different place instead of the first page in .Rmd pdf. I found the function render_toc() from #Garrick Aden-Buie this is the link https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8. It is working perfect if I don't use cat() in chunk codes.
I have to use cat() in my .Rmd, is there any way I can add the table of contents any place in .Rmd
You can see the table of contents only test 1, test 2, and test 3, setosa, versicolor, virginica did not include in table of content
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: yes
toc_depth: 4
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("render_toc.R")
library(knitr)
```
\newpage
# Table of Contents {#crazy-slug-here}
```{r echo = FALSE}
render_toc("test.Rmd")
```
# Test 1
# Test 2
# Test 3
```{r echo = FALSE, results ='asis'}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,], aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat('\n')
}
```
Using the code you provided I'm able to knit the pdf document and get the three plots, one for each Species in iris.
This code, however, produces two TOCs: one of them in the first page which is not desired according to your question. In order to get only the TOC produced by the function render_toc() set toc: no in the YAML header.
---
title: "test"
output:
pdf_document:
number_sections: yes
toc: no
toc_depth: 4
---
This way only one TOC will be generated and placed where render_toc() is.
Edit: After seeing your edit with the desired output, I think the following LaTeX code can help:
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
Note: here we are not using render_toc(). The code creates the TOC in the second page of the document. If you need another blank page, just introduce another block of:
\newpage
\thispagestyle{plain}
\mbox{}
These pages may or may not be numbered, which you control via the argument passed to \thispagestyle{}. See here for more details.
Full code:
---
title: "Document title"
output:
pdf_document:
number_sections: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
\thispagestyle{plain}
\mbox{}
\setcounter{tocdepth}{2}
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
\newpage
\thispagestyle{plain}
\mbox{}
# Test 1
## Subsection 1
# Test 2
## Subsection 2
# Test 3
```{r, results='asis', echo=FALSE}
library(ggplot2)
for(Species in levels(iris$Species)){
cat('\n#', Species, '\n')
p <- ggplot(iris[iris$Species == Species,],
aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
print(p)
cat("\n")
}
```
This way we end up with a TOC looking like this:
When I want to print some tables in html report from knitr, sometime I use knitr::kable(), and other times I use htmltable::htmltable.
When I use bookdown::html_document2 in the YAML numbering of tables from kable is automatic. However, it is not for htmltable, and I need to use options(table_counter = TRUE), which generates another numbering mechanism. Is there a way to unify it?
Example:
---
title: "Untitled"
author: "Guilherme"
date: "10/26/2020"
output:
bookdown::html_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(table_counter = TRUE)
```
```{r cars}
library(htmlTable)
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
library(knitr)
kable(mtcars[1:5,1:5],
caption = "XX")
```
Outputs:
Thanks!