2016-05-24 6 views
1

ループ内から各ページを作成し、生成された各ページにdygraphを含むflexdashboardを生成しようとしています(HTMLウィジェットは同じ)。ループ内にmarkdownコメントとhtml wigetsを生成する

私は広範に見てきましたが、cat("title")generate markdown comments within 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) 
} 
+0

これは現在可能ではないようです(https://github.com/rstudio/flexdashboard/issues/80を参照)。 – user338714

+0

私は何か作業をすることができました。私は上記のgithubの議論に応えたが、私の答えはここに再投稿する。 –

答えて

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

    } 

    } 

} 


``` 
+0

これは '==='と '---'構文の代わりに異なるヘッダーレベルを使うことができるので、これを改訂しようとしています。 –

1

私はr paste(knitr::knit(text = out))でインラインそれらをknitingその後、明示的にRチャンクを構築することにより、コンテンツを自動生成することができました。この驚くべきコード行はSO postにあります。

私のケースでは、別々のタブで、内容の異なる一連のグラフを作成したかったのです。それぞれのグラフは似ていましたが、数が多く(約15)、別々のチャンクをすべてコピー/ペーストしたくありませんでした。より簡単な例の

Here is a gist you can downloadです。 (以下のコードもありますが、各チャンクの前に\を追加して、1つのコードブロックとしてレンダリングするので、実行前に\を削除してください))グラフを作成するのにもっと複雑な関数を構築しました。 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')))` 
関連する問題