1

I'm trying to generate a flexdashboard, creating each page from within a loop and with each of the generated pages containing a dygraph (although any HTML widget ought to behave the same).

I have looked extensively and it seems that rmarkdown comments can be generated using cat("title") (as per the solution here: generate markdown comments within for loop).

The HTML widgets on the other hand only behave nicely if you use htmltools::tagList() (as per the solution here:For loop over dygraph does not work in R).

I dont have working code to share, but this broadly gives the picture of what I am hoping to achieve:

for (i in 1:ncol(downloadedData)){
fund_NAVS <- downloadedData[,i] #this is an xts object
fund_NAVS <- fund_NAVS[!is.na(fund_NAVS)]
cat("pageTitle")
cat("===================================== \n")
cat("Row\n")
cat("------------------------------------- \n")
cat("### Page title")
dygraph(fund_NAVS)
}
Community
  • 1
  • 1
Dan
  • 11
  • 3

2 Answers2

2

I've been able to get this to partially work using pander::pandoc.header. However, getting content (plots and HTML objects) to actually show up in the final HTML file is a different story.

---
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    source: embed
    vertical_layout: fill
---

```{r results='asis'}

library(pander)

pages <- 5
cols <- 2
sections <- 3

for (p in 1:pages) {

  pandoc.header(paste("Page", p), level = 1)

  for (c in 1:cols) {

    pandoc.header(paste("Column", c), level = 2)

    for (s in 1:sections) {

      pandoc.header(paste("Section", s), level = 3)
      cat("hi")
      pandoc.p("")

    }

  }

}


```
Jeff Keller
  • 771
  • 1
  • 7
  • 15
  • Going to revise this because you can simply use different header levels as a replacement for the `===` and `---` syntax. – Jeff Keller Aug 12 '16 at 18:30
2

I was able to autogenerate content by building r chunks explicitly then kniting them inline with r paste(knitr::knit(text = out)). This amazing line of code was found in an SO post.

In my case, I wanted to produce a series of graphs, each with a separate tab, with different content. Each graph was similar but there were numerous (about 15) and I didn't want to copy/paste all of the separate chunks.

Here is a gist you can download of a more simple example. (The code is also below but note that I add \ before each chunk so that it rendered as a single block of code so remove the \ before running.) I built a much more complicated function to build graphs, but the idea of the R chunks can be carried forward to any list object containing htmlwidgets as elements.

---
title: "Loop to Auto Build Tabs Containing htmlwidgets"
output: flexdashboard::flex_dashboard
---

\```{r setup, echo =FALSE, eval = TRUE}
library(tidyverse)
library(flexdashboard)
library(highcharter)

labels <- mtcars %>% names # these will serve as labels for each tab

# create a bunch of random, nonsensical line graphs
hcs <- purrr::map(.x = mtcars, ~highcharter::hchart(mtcars, y = .x, type = 'line')) %>%
    setNames(labels) # assign names to each element to use later as tab titles
\```

Page
====================

Column {.tabset .tabset-fade}
-----------------------------

<!-- loop to build each tabs (in flexdashboard syntax) -->
<!-- each element of the list object `out` is a single tab written in rmarkdown -->
<!-- you can see this running the next chunk and typing `cat(out[[1]])` -->

\```{r, echo = FALSE, eval = TRUE}

out <- lapply(seq_along(hcs), function(i) {

  a1 <- knitr::knit_expand(text = sprintf("### %s\n", names(hcs)[i])) # tab header, auto extracts names of `hcs`
  a2 <- knitr::knit_expand(text = "\n```{r}") # start r chunk
  a3 <- knitr::knit_expand(text = sprintf("\nhcs[[%d]]", i)) # extract graphs by "writing" out `hcs[[1]]`, `hcs[[2]]` etc. to be rendered later
  a4 <- knitr::knit_expand(text = "\n```\n") # end r chunk

  paste(a1, a2, a3, a4, collapse = '\n') # collapse together all lines with newline separator

})

\```

<!-- As I mentioned in the SO post, I don't quite understand why it has to be -->
<!-- 'r paste(knitr::knit(...)' vs just 'r knitr::knit(...)' but hey, it works -->

`r paste(knitr::knit(text = paste(out, collapse = '\n')))`
Community
  • 1
  • 1
Danton Noriega
  • 686
  • 8
  • 12