1

我正在尝试生成一个 flexdashboard,从一个循环中创建每个页面,并且每个生成的页面都包含一个dygraph(尽管任何 HTML 小部件都应该表现相同)。

我已经广泛查看,似乎rmarkdown可以使用生成评论cat("title")(根据这里的解决方案:在 for loop 中生成降价评论)。

另一方面,HTML 小部件只有在您使用时才会表现良好htmltools::tagList()(根据此处的解决方案:For loop over dygraph does not work in R)。

我没有可以共享的工作代码,但这大致说明了我希望实现的目标:

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)
}
4

2 回答 2

2

我已经能够使用pander::pandoc.header. 但是,让内容(绘图和 HTML 对象)实际显示在最终的 HTML 文件中是另一回事。

---
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("")

    }

  }

}


```
于 2016-08-12T17:39:56.097 回答
2

我能够通过显式构建 r 块然后将它们与r paste(knitr::knit(text = out)). 这个惊人的代码行是在一个SO 帖子中找到的。

就我而言,我想制作一系列图表,每个图表都有一个单独的选项卡,内容不同。每个图表都很相似,但有很多(大约 15 个),我不想复制/粘贴所有单独的块。

这是一个要点,您可以下载一个更简单的示例。(代码也在下面,但请注意我\在每个块之前添加,以便它呈现为单个代码块,因此\在运行之前删除。)我构建了一个更复杂的函数来构建图形,但是 R 块的想法可以被结转到包含 htmlwidgets 作为元素的任何列表对象。

---
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')))`
于 2016-09-14T20:22:36.090 回答