2017-07-06 10 views
1

次のコードを使用して、Rmarkdownにテーブルを含む動的タブを作成しています。Rmarkdownで動的なタブとテーブルを使用する場合

# TEST 
``` {r echo=FALSE, results = 'asis', message = FALSE, warnings = FALSE} 
print_month <- function(month) { 
    cat(" \n##", format(month, "%B"), " results \n") 
    print(knitr::kable(data.frame(A = c(1,2,3), B = c(1,2,3)))) 
    cat(" \n") 
} 

seq.Date(from = ymd(20170101), to = ymd(20170601), by = 'month') %>% 
    purrr::walk(print_month) 
``` 

これまで私はそれを見てきましたが、時にはそれが失敗する理由を絞り込むことはできません。それが失敗した場合には、この enter image description here

の表は、私がHTMLコードに見ると段落ですが、正常に動作したときに、それをテーブルとして描画するか...

答えて

2

のように見えるかもしれないprint()cat()機能かもしれませんお互いに交流している?

私は、組み立てられた単一の文字列を返す関数を優先し、呼び出し元に出力方法を決定させるようにします。

library(magrittr) 
library(lubridate) 
assemble_month <- function(month) { 
    d <- mtcars[1:5, 1:6] #data.frame(A = c(1,2,3), B = c(1,2,3)) 
    html_table <- knitr::kable(d, format = "html") 
    paste0(
    "\n##", format(month, "%B"), " results\n", 
    html_table, 
    "\n" 
) 
} 

seq.Date(from = ymd(20170101), to = ymd(20170601), by = 'month') %>% 
    purrr::map_chr(assemble_month) %>% 
    cat() 

enter image description here

私のアプローチの欠点は、しかし道はknitrの素敵な値下げツーHTML CSSフォーマットを活用するために失敗し、それはHTMLテーブルを出力しています。私は一般的にそうhtml_table

enter image description here

html_table <- mtcars[1:5, 1:6] %>% 
    knitr::kable(format = "html") %>% 
    kableExtra::kable_styling(
     bootstrap_options = c("striped", "hover", "condensed", "responsive"), 
     full_width = F 
    ) 
となり、 kableExtraバックスタイルを追加します(私はそれがより現実的に見えるように大きな例の表を使用していました。)

関連する問題