My question is an expansion of the Generate Dynamic R Markdown Blocks question applied to R flexdashboards.
I made a simple flexdashboard with one numeric input: the number of columns to be rendered in the dashboard body. As I modify this numeric input I expect the number of columns to change accordingly.
For the static case, where the number of columns is set at the beginning, the solution was provided in the referred question.
For the dynamic case, where the number of columns is set by
shiny::numericInput()
, I tried modifying this solution by wrapping the R code inreactive({})
statements.
Here is my flexdashboard.Rmd file:
---
title: "Dynamic columns"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
---
```{r setup, include=FALSE}
library(shiny)
library(knitr)
library(flexdashboard)
```
Column {.sidebar data-width=130}
-----------------------------------------------------------------------
```{r}
numericInput(
"ncols",
label = "number of columns",
value = 3
)
```
```{r column_generator, include=FALSE}
output <- reactive({
out <- NULL
for (i in 1:input$ncols) {
out <- c(out, knit_expand(text = "### column: {{i}}"))
}
out
})
```
Column {data-width=650}
-----------------------------------------------------------------------
`r reactive({paste(knit(text = output()), collapse = '\n')})`
I believe I'm close to the solution because I get the following output in my flexdashboard body:
column: 1 ### column: 2 ### column: 3
However, this text is not rendered into three columns by knit()
.