Generate Dynamic R Markdown Blocks

Generate Dynamic R Markdown Blocks

Try knit_expand():

Automate Chunks of Analysis in R Markdown 
========================================================

```{r setup, echo=FALSE}
library(knitr)
```

```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand(text='#### Species = {{i}}'))
}
```

`r paste(knit(text = out), collapse = '\n')`

You can also create a template file like 'child.rmd' and put this in your for loop so you don't have to put a complicated analysis in quotes:

out = c(out, knit_expand('template.rmd'))

Then have your 'template.rmd' be:

#### Species = {{i}}

Generate dynamic R-Markdown blocks with data.table objects

It works for me if I also generate the R blocks:

---
title: "R Notebook"
output:
html_document:
df_print: paged
---

```{r setup, echo=FALSE}
library(knitr)
library(data.table)
iris.dt <- data.table(iris)
iris.species <- as.character(unique(iris.dt$Species))
```

### View by Species automated {.tabset .tabset-fade .tabset-pills}
```{r run-numeric-md, include=FALSE}
out <- vector(mode = "character", length = length(iris.species))
for (i in iris.species) {
out[i] <- knit_expand(text = c("#### {{stringr::str_to_title(i)}}",
"```{r, echo = FALSE}",
"iris.dt[Species == '{{i}}']",
"```"))
}
```

`r paste(knit(text = out), collapse = '\n')`

Create new RMarkdown blocks for each item generated in a loop

Using results='asis' and inserting markdown syntax before and after each plot:

```{r, results='asis'}
for(cyls in sort(unique(mtcars$cyl))) {
cat(paste0("## ", cyls, " cylinders\n\n"))
p = mtcars %>%
filter(cyl == cyls) %>%
ggplot(aes(x = mpg, y = hp)) +
geom_point() +
labs(title = paste(cyls,"cylinders"))
print(p)

cat("\n\n---------------------\n\n")
}
```

How to pass a reactive plot generated in Shiny to Rmarkdown to generate dynamic reports

Basically your question already included all the building blocks. I only updated the report template to include the code to plot the radar chart. As a parameter I decided to pass the filtered dataset. In the server I only adjusted the specs for the params:

server <- function(input, output) {
output$plot1 <- renderChartJSRadar({
chartJSRadar(skills[, c("Label", input$selectedPeople)],
maxScale = 10, showToolTipLabel=TRUE)
})

output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)

params <- list(scores = skills[, c("Label", input$selectedPeople)])

rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}

Report.Rmd

---
title: "Dynamic report"
output: html_document
params:
scores: NA
---

```{r}
chartJSRadar(params$scores, maxScale = 10, showToolTipLabel=TRUE)
```

Sample Image

Dynamic RMarkdown Title from R Code Chunk

You can insert arbitrary R code anywhere you want in an Rmarkdown document (including the title) by surrounding the block with ` ticks and putting an r in front of the code:

So this (note the ; between lines of code):

---
author: "Test"
date: "September 27, 2018"
output: html_document
title: '`r days <- 60; paste0(days, " Days")`'
---

Knits as this:

Sample Image


As @camille pointed out, you can also include yaml blocks later in the file by surrounding them with the same --- as in the initial header. This lets you make use of variables defined later in the code:

You can also include R chunks inline in markdown and use R expressions to control the display of markdown:

---
author: "Test"
date: "September 27, 2018"
output: html_document
---

```{r}
debug <- 2
num1 <- 3
```

`r if(debug > 3){"## Debug is > 3"}`
`r if(debug < 3){"## Debug is < 3"}`

The value of num1 is `r num1`

---
title: '`r paste0('Title: the value of debug is ', debug)`'
---

Renders as this:

Sample Image



Related Topics



Leave a reply



Submit