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:
Related
This question is similar, but not identical to this one.
Basically, I have a number of tables that I would like to show in tabsets using DT::datatable(). Unfortunately, I can't figure out how.
The following code works, but I need to manually type all the code:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
### table no. 1
```{r}
#| results: asis
datatable(mtcars)
```
### table no. 2
```{r}
#| results: asis
datatable(mtcars)
```
:::
The following works, but instead of datatable() uses a simple markdown table from pander which does not give the desired effect.
---
title: "Untitled"
format: html
---
```{r}
library(pander)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
cat(pander(mtcars))
}
```
:::
The following code does not work, and I don't know how to make it work:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(datatable(mtcars))
}
```
:::
This is a known issue in the context of RMarkdown. But as it turns out the solution for RMarkdown also works for Quarto.
Following this answer related to the same issue in RMarkdown [Discalimer: The answer is from me] you could achieve your desired result by first making sure that the javascript dependencies needed for DT are included in your document and by wrapping the datatable call inside htmltools::tagList:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
```{r, include=FALSE}
# Init Step to make sure that the dependencies are loaded
htmltools::tagList(datatable(mtcars))
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(htmltools::tagList(datatable(mtcars)))
}
```
:::
I am new to RMarkdown and Beamer. I am trying to make a set of slides where each slide contains a JPG. I have a dozen to loop through. How do I set this up in a loop?
Here is the RMD file:
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
---
# Introduction
## Blah
- Text
- Here
- Etc.
# Images
## pic_1
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_1.jpg")
```
## pic_2
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_2.jpg")
```
## pic_3
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_3.jpg")
```
## pic_4
```{r echo=FALSE, out.width = '100%'}
knitr::include_graphics("../images/modelA_pic_4.jpg")
```
I know I can put the slide titles and figure paths into a data frame, but I am not sure how to do this inside RMarkdown and how to loop through it to build the slide titles and insert the images.
title <- c('pic_1', 'pic_2', 'pic_3', 'pic_4')
fpath <- c('modelA_pic_1.jpg', 'modelA_pic_2.jpg', 'modelA_pic_3.jpg', 'modelA_pic_4.jpg')
fpath <- paste0("../images/", fpath)
myfiles <- data.frame(title, fpath)
Updated Based on Accepted Answer
Below is what I ended up using for my Rmd. This page explains the basics of the xmpmulti package.
For this set up, my RMD is in one folder; the images are one folder up (../) and then in a folder called temp (../temp/). The images in this folder are named test-1.png, test-2.png, etc.
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
header-includes:
- \usepackage{xmpmulti}
---
# Introduction
## Blah
- Text
- Here
- Etc.
```{=latex}
\end{frame}
\section{Images}
\begin{frame}
\frametitle<1>{picture 1}
\frametitle<2>{picture 2}
\centering
\multiinclude[format=png,start=1,end=2,graphics={width=1\textwidth}]{../temp/test}
\end{frame}
\begin{frame}
```
some test
Assuming your images are named pic-1.png etc., then beamer has an automatic way to loop over the images via the xmpmulti package:
---
title: 'slideshow'
output:
beamer_presentation:
theme: "AnnArbor"
keep_tex: true
header-includes:
- \usepackage{xmpmulti}
---
# Introduction
## Blah
- Text
- Here
- Etc.
```{=latex}
\end{frame}
\section{Images}
\begin{frame}
\frametitle<1>{picture 1}
\frametitle<2>{picture 2}
\frametitle<3>{picture 3}
\centering
\multiinclude[format=png,start=1,end=3,graphics={width=.6\textwidth}]{pic}
\end{frame}
\begin{frame}
```
some test
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.
I have the r markdown document below. I would like to hide page 2 if parameter "P" is not equal to A.
So the result would be if parameter P != A then only 3 pages are produced.
Is this possible somehow?
---
title: "Untitled"
output:
pdf_document:
toc: yes
params:
P: A
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\newpage
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
## PAGE3
this is text for page 3
```{r pressure, echo=FALSE}
plot(pressure)
```
\newpage
## PAGE4
this is text for page 4
You could comment the section out based on P value. Here is a bit of a hackish way to do it: surround the section with \iffalse/\fi. Note that the R code inside the section still needs to be valid for this to work (it will be compiled by knitr, but ignored by latex).
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\iffalse")
```
## PAGE2
this is text for page 2
this is text for page 2
```{r cars}
summary(cars)
```
\newpage
```{r, echo=FALSE, results='asis'}
if(params$P != "A")
cat("\\fi")
```
I'm trying to produce a flexdashboard with R and want to show code in my presentation, but this seems not to work.
Here a little example:
---
title: "Test"
output:
flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(flexdashboard)
```
### Code
```{r, eval=FALSE, include=TRUE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Output
```{r, fig.align='center', echo = FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
Even other chuck options like fig.show = 'hide' will not work.
Is it possible to show code in a R-chuck in flexdashboard?
The code highlights would be a benefit instead of a plain text.
If you want both the code and plot to show set the chunk options to: echo = true
If you just want code set it to: echo=TRUE, eval=FALSE
---
title: "Test"
output:
flexdashboard::flex_dashboard
---
```{r setup, include=FALSE}
library(flexdashboard)
```
### Code
```{r, echo=TRUE, eval=FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Code and Plot
```{r, echo=TRUE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
### Plot
```{r, fig.align='center', echo = FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
It won't show as a panel in your presentation, but to add a </> Source Code button to your dashboard, include source_code: embed in your YAML.
---
title: "Example"
output:
flexdashboard::flex_dashboard:
source_code: embed
---